Oracle TopLink Developer's Guide
10g Release 3 (10.1.3) B13593-01 |
|
![]() Previous |
![]() Next |
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:
Mapping to a Single Text Node With an XML Composite Direct Collection Mapping
Mapping to a Single Attribute With an XML Composite Direct Collection Mapping
Mapping to a List of Unions With an XML Composite Direct Collection Mapping
Mapping to a Union of Lists With an XML Composite Direct Collection Mapping
Specifying the Content Type of a Collection With an XML Composite Direct Collection Mapping
See Chapter 68, "Configuring an XML Composite Direct Collection Mapping" for more information.
This section describes using a composite direct collection XML mapping when:
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>
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>
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>
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
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>
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
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);
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
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);
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
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);