Skip Headers

# XML Any Collection Mapping

Previous

Next

The XML any collection mapping is similar to the composite collection XML mapping (see "XML Composite Collection Mapping"), except that the referenced objects may be of different types (including `String`). These types need not be related to each other through inheritance or a common interface.

The corresponding object attribute value can be an instance of any object with a `Descriptor`, a `java.lang.Object`, a `java.lang.String`, a primitive object (such as `java.lang.Integer`), or a user defined type generic enough for all possible application values.

This mapping is useful with the following XML schema constructs:

- any

- choice

- substitution groups

Each of the referenced objects (except `String`) must specify a default root element on their descriptor (see "Default Root Element").

Given the XML schema in Example: Schema for XML Any Collection Mapping, Figure: Java Classes for XML Any Collection Mapping illustrates the Java classes used in this example. A single XML any collection mapping is used to map `Customer` attribute `contactMethods`. This attribute must be generic enough to reference all possible values: in this example, instances of `Address`, `PhoneNumber`, and `String`.

Schema for XML Any Collection Mapping

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="customer" type="customer-type"/>
        <xsd:complexType name="customer-type">
            <xsd:sequence>
                <xsd:element name="contact-methods" type="xsd:anyType"/>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:element name="address">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="street" type="xsd:string"/>
                    <xsd:element name="city" type="xsd:string"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
        <xsd:element name="phone-number" type="xsd:string"/>
    </xsd:schema>

Java Classes for XML Any Collection Mapping

Description of "Java Classes for XML Any Collection Mapping"

Figure: XML Any Collection Mapping illustrate how the XML any collection mapping maps to a collection of `Address`, `PhoneNumber`, and `String` objects in an XML document that conforms to the schema in Example: Schema for XML Any Collection Mapping.

XML Any Collection Mapping

Description of "XML Any Collection Mapping"

Example: Java for XML Any Collection Mapping shows how to configure this mapping in Java.

Java for XML Any Collection Mapping

    XMLAnyCollectionMapping contactMethodsMapping = new XMLAnyCollectionMapping();
    contactMethodsMapping.setAttributeName("contactMethods");
    contactMethodsMapping.setXPath("contact-methods");

For more information about TopLink XML mapping support for `xs:any` and `xs:anyType`, see "xs:any and xs:anyType Support".

See Configuring an XML Any Collection Mapping for more information.