Skip Headers

# XML Any Object Mapping

Previous

Next

The XML any object mapping is similar to the composite object XML mapping (see "XML Composite Object Mapping") except that the reference object may be of any type (including `String`). This type does not need to be related to any other particular type 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

Referenced objects can specify a default root element on their descriptor (see "Default Root Element").

Note: The undefined document root element of a referenced object is ignored during marshalling with an any collection mapping and object mapping.

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

Schema for XML Any Object 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-method" 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 Object Mapping

Description of "Java Classes for XML Any Object Mapping"

Figure: XML Any Object Mapping to Address Type, Figure: XML Any Object Mapping to PhoneNumber Type, and Figure: XML Any Object Mapping to String Type illustrate how the XML any object mapping maps to an `Address`, `PhoneNumber`, and `String` (respectively) in XML documents that conform to the schema in Example: Schema for XML Any Object Mapping.

XML Any Object Mapping to Address Type

Description of "XML Any Object Mapping to Address Type"

XML Any Object Mapping to PhoneNumber Type

Description of "XML Any Object Mapping to PhoneNumber Type"

XML Any Object Mapping to String Type

Description of "XML Any Object Mapping to String Type"

Example: Java for XML Composite Collection Mapping for a Collection Attribute shows how to configure this mapping in Java.

Java for XML Any Object Mapping

    XMLAnyObjectMapping contactMethodMapping = new XMLAnyObjectMapping();
    contactMethodMapping.setAttributeName("contactMethod");
    contactMethodMapping.setXPath("contact-method");

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 Object Mapping for more information.