Skip Headers

# Mapping to Different Elements by Element Position

Previous

Next

An object may have multiple composite object mappings to the same reference class. Each composite object mapping must have a unique XPath. This example uses unique XPaths by position.

Given the XML schema in Example: Schema for XML Composite Object Mapping to Elements by Name, Figure: XML Composite Object Mapping to Elements by Name illustrates an XML composite object mapping to different elements by position in a corresponding XML document. Example: Java for XML Composite Object Mapping to Elements by Name shows how to configure this mapping in Java.

Schema for XML Composite Object Mapping to Elements by Position

    <?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="first-name" type="xsd:string"/>
                <xsd:element name="last-name" type="xsd:string"/>
                <xsd:element name="address" maxOccurs="2">
                    <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:sequence>
        </xsd:complexType>
    </xsd:schema>

XML Composite Object Mapping to Elements by Position

Description of "XML Composite Object Mapping to Elements by Position"

Java for XML Composite Object Mapping to Elements by Position

    XMLCompositeObjectMapping billingAddressMapping = new XMLCompositeObjectMapping();
    billinAddressMapping.setAttributeName("billingAddress");
    billinAddressMapping.setXPath("address[1]");
    billinAddressMapping.setReferenceClass(Address.class);

    XMLCompositeObjectMapping shippingAddressMapping = new XMLCompositeObjectMapping();
    shippingAddressMapping.setAttributeName("shippingAddress");
    shippingAddressMapping.setXPath("address[2]");
    shippingAddressMapping.setReferenceClass(Address.class);