| Oracle® TopLink Developer's Guide 10g (10.1.3.1.0) B28218-01 |
|
![]() 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 62-51, Figure 62-28 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.
Example 62-51 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>
Figure 62-28 Java Classes for XML Any Object Mapping

Figure 62-29, Figure 62-30, and Figure 62-31 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 62-51.
Figure 62-29 XML Any Object Mapping to Address Type

Figure 62-30 XML Any Object Mapping to PhoneNumber Type

Figure 62-31 XML Any Object Mapping to String Type

Example 62-49 shows how to configure this mapping in Java.
Example 62-52 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 Chapter 68, "Configuring an XML Any Object Mapping" for more information.