Skip Headers
Oracle TopLink Developer's Guide
10g Release 3 (10.1.3)
B13593-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

XML Composite Direct Collection Mapping

XML composite direct collection mappings map a Java collection of simple object attributes to XML attributes and text nodes. Use multiplicity settings to specify an element as a collection. The XML schema allows you to define minimum and maximum occurrences. You can use a composite direct collection XML mapping in the following scenarios:

See Chapter 68, "Configuring an XML Composite Direct Collection Mapping" for more information.

Mapping to Multiple Text Nodes

This section describes using a composite direct collection XML mapping when:

Mapping to a Simple Sequence

Given the XML schema in Example 65-24, Figure 65-15 illustrates a composite direct collection XML mapping to a simple sequence of text nodes in a corresponding XML document. Example 65-25 shows how to configure this mapping in Java.

Example 65-24 Schema for Composite Direct Collection XML Mapping to a Simple Sequence

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee" type="employee-type"/>
    <xsd:complexType name="employee-type">
        <xsd:sequence>
            <xsd:element name="task" type="xsd:string" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Figure 65-15 Composite Direct Collection XML Mapping to a Simple Sequence

This illustaration is described in the preceeding paragraph.

Example 65-25 Java for Composite Direct Collection XML Mapping to a Simple Sequence

XMLCompositeDirectCollectionMapping tasksMapping = new XMLCompositeDirectCollectionMapping();
tasksMapping.setAttributeName("tasks");
tasksMapping.setXPath("task/text()");

Mapping to a Sequence in a Subelement

Given the XML schema in Example 65-26, Figure 65-16 illustrates a composite direct collection XML mapping to a sequence of text nodes in a subelement in a corresponding XML document. Example 65-27 shows how to configure this mapping in Java.

Example 65-26 Schema for Composite Direct Collection XML Mapping to a Subelement Sequence

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee" type="employee-type"/>
    <xsd:complexType name="employee-type">
        <xsd:sequence>
            <xsd:element name="tasks">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="task" type="xsd:string" maxOccurs="unbounded"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Figure 65-16 Composite Direct Collection XML Mapping to a Subelement Sequence

This illustaration is described in the preceeding paragraph.

Example 65-27 Java for Composite Direct Collection XML Mapping to a Subelement Sequence

XMLCompositeDirectCollectionMapping tasksMapping = new XMLCompositeDirectCollectionMapping();
tasksMapping.setAttributeName("tasks");
tasksMapping.setXPath("tasks/task/text()");

Mapping to Multiple Attributes

Given the XML schema in Example 65-28, Figure 65-17 illustrates a composite direct collection XML mapping to a sequence of text nodes in a subelement in a corresponding XML document. Example 65-29 shows how to configure this mapping in Java.

Example 65-28 Schema for Composite Direct Collection XML Mapping to Multiple Attributes

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee" type="employee-type"/>
    <xsd:complexType name="employee-type">
        <xsd:sequence>
            <xsd:element name="tasks" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:attribute name="task" type="xsd:string"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Figure 65-17 Composite Direct Collection XML Mapping to Multiple Attributes

This illustaration is described in the preceeding paragraph.

Example 65-29 Java for Composite Direct Collection XML Mapping to Multiple Attributes

XMLCompositeDirectCollectionMapping tasksMapping = new XMLCompositeDirectCollectionMapping();
tasksMapping.setAttributeName("tasks/@task");
tasksMapping.setXPath("task/text()");

Mapping to a Single Text Node With an XML Composite Direct Collection Mapping

When you map a collection to a single node, the contents of the node is treated as a space-separated list.

Given the XML schema in Example 65-30, Figure 65-18 illustrates a composite direct collection XML mapping to a single text node in a corresponding XML document. Example 65-31 shows how to configure this mapping in Java.

Example 65-30 Schema for XML Composite Direct Collection Mapping to a Single Text Node

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee" type="employee-type"/>
    <xsd:complexType name="employee-type">
        <xsd:sequence>
            <xsd:element name="tasks" type="tasks-type"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:simpleType name="tasks-type">
        <xsd:list itemType="xsd:string"/>
    </xsd:simpleType>
</xsd:schema>

Figure 65-18 XML Composite Direct Collection Mapping to a Single Text Node

Description of Figure 65-18  follows
Description of "Figure 65-18 XML Composite Direct Collection Mapping to a Single Text Node"

Example 65-31 Java for XML Composite Direct Collection Mapping to a Single Text Node

XMLCompositeDirectCollectionMapping tasksMapping = new XMLCompositeDirectCollectionMapping();
tasksMapping.setAttributeName("tasks");
tasksMapping.setXPath("tasks/text()");
tasksMapping.setUsesSingleNode(true);

Mapping to a Single Attribute With an XML Composite Direct Collection Mapping

Given the XML schema in Example 65-32, Figure 65-19 illustrates a composite direct collection XML mapping to a single attribute in a corresponding XML document. Example 65-33 shows how to configure this mapping in Java.

Example 65-32 Schema for XML Composite Direct Collection Mapping to a Single Attribute

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee" type="employee-type"/>
    <xsd:complexType name="employee-type">
        <xsd:attribute name="tasks" type="tasks-type"/>
    </xsd:complexType>
    <xsd:simpleType name="tasks-type">
        <xsd:list itemType="xsd:string"/>
    </xsd:simpleType>
</xsd:schema>

Figure 65-19 XML Composite Direct Collection Mapping to a Single Attribute

This illustaration is described in the preceeding paragraph.

Example 65-33 Java for XML Composite Direct Collection Mapping to a Single Attribute

XMLCompositeDirectCollectionMapping tasksMapping = new XMLCompositeDirectCollectionMapping();
tasksMapping.setAttributeName("tasks");
tasksMapping.setXPath("@tasks");
tasksMapping.setUsesSingleNode(true);

Mapping to a List of Unions With an XML Composite Direct Collection Mapping

Given the XML schema in Example 65-34, Figure 65-20 illustrates a composite direct collection XML mapping to a list of unions in a corresponding XML document. Example 65-35 shows how to configure this mapping in Java.

Example 65-34 Schema for XML Composite Direct Collection Mapping to List of Unions

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="vacation" type="listOfUnions"/>
    <xsd:simpleType name="listOfUnions">
        <xsd:list>
            <xsd:simpleType>
                <xsd:union memberTypes="xsd:date xsd:integer"/>
            </xsd:simpleType>
        </xsd:list>
    </xsd:simpleType>
</xsd:schema>

Figure 65-20 Composite XML Direct Collection Mapping to List of Unions

Description of Figure 65-20  follows
Description of "Figure 65-20 Composite XML Direct Collection Mapping to List of Unions"

Example 65-35 Java for XML Composite Direct Collection Mapping to List of Unions

XMLCompositeDirectCollectionMapping mapping = new XMLCompositeDirectCollectionMapping();
mapping.setAttributeName("myattribute");
XMLUnionField field = new XMLUnionField("listOfUnions/text()");
mapping.addSchemaType(new Qname(url,"int"));
mapping.addSchemaType(new Qname(url,"date"));
mapping.setField(field);
mapping.useSingleElement(false);

Mapping to a Union of Lists With an XML Composite Direct Collection Mapping

Given the XML schema in Example 65-34, Figure 65-20 illustrates an XML composite direct collection mapping to a list of unions in a corresponding XML document. Example 65-35 shows how to configure this mapping in Java.

Example 65-36 Schema for XML Composite Direct Collection Mapping to a Union of Lists

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="vacation" type="unionOfLists"/>
    <xsd:simpleType name="unionOfLists">
        <xsd:union memberTypes="xsd:double">
            <xsd:simpleType>
                <xsd:list itemType="xsd:date"/>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:list itemType="xsd:integer"/>
            </xsd:simpleType>
        </xsd:union>
    </xsd:simpleType>
</xsd:schema>

Figure 65-21 XML Composite Direct Collection Mapping to a Union of Lists

Description of Figure 65-21  follows
Description of "Figure 65-21 XML Composite Direct Collection Mapping to a Union of Lists"

Note that in this example, valid XML documents contain either all xsd:double, all xsd:date, or all xsd:integer values.

Example 65-37 Java for XML Composite Direct Collection Mapping to a Union of Lists

XMLCompositeDirectCollectionMapping mapping = new XMLCompositeDirectCollectionMapping();
mapping.setAttributeName("myattribute");
mapping.useSingleElement(false);
XMLUnionField unionField = new XMLUnionField("UnionOfLists/text()");
field.addSchemaType(new Qname(url," integer"))
field.addSchemaType (new Qname(url," date"))
field.addSchemaType (new Qname(url," double"))
field.setUsesSingleNode(false);

Specifying the Content Type of a Collection With an XML Composite Direct Collection Mapping

By default, TopLink will treat the node values read by a composite direct collection XML mapping as objects of type String. You can override this behavior by specifying the type of the collection's contents.

Given the XML schema in Example 65-38, Figure 65-22 illustrates an XML composite direct collection mapping to a simple sequence in a corresponding XML document. The mapping is configured to specify the content type of the collection as Calendar. Example 65-39 shows how to configure this mapping in Java.

Example 65-38 Schema for XML Composite Direct Collection Mapping with Specified Content Type

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee" type="employee-type"/>
    <xsd:complexType name="employee-type">
        <xsd:sequence>
            <xsd:element name="vacation" type="xsd:string" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Figure 65-22 XML Composite Direct Collection Mapping with Specified Content Type

Description of Figure 65-22  follows
Description of "Figure 65-22 XML Composite Direct Collection Mapping with Specified Content Type"

Example 65-39 Java for XML Composite Direct Collection Mapping with Specified Content Type

XMLCompositeDirectCollectionMapping tasksMapping = new XMLCompositeDirectCollectionMapping();
tasksMapping.setAttributeName("vacationDays");
tasksMapping.setXPath("vacation/text()");
tasksMapping.setAttributeElementClass(Calendar.class);