Skip Headers

# Mapping to a Specified Schema Type

Previous

Next

In most cases, TopLink can determine the target format in the XML document. However, there are cases where you must specify which one of a number of possible targets TopLink should use. For example, a `java.util.Calendar` could be marshalled to a schema `date`, `time`, or `dateTime` node, or a `byte[]` could be marshalled to a schema `hexBinary` or `base64Binary` node.

Given the XML schema in Example: Schema for XML Direct Mapping to Text Node by Position, Figure: XML Direct Mapping to Text Node by Position illustrates an XML direct mapping to a text node by position in a corresponding XML document. Example: Java for XML Direct Mapping to Text Node by Position shows how to configure this mapping in Java.

Schema for XML Direct Mapping to a Specified Schema Type

    <?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="picture" type="xsd:hexBinary"/>
                <xsd:element name="resume" type="xsd:base64Binary"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

XML Direct Mapping to a Specified Schema Type

Description of "XML Direct Mapping to a Specified Schema Type"

Java for XML Direct Mapping to a Specified Schema Type

    XMLDirectMapping pictureMapping = new XMLDirectMapping();
    pictureMapping.setAttributeName("picture");
    pictureMapping.setXPath("picture/text()");
    XMLField pictureField = (XMLField) pictureMapping.getField();
    pictureField.setSchemaType(XMLConstants.HEX_BINARY_QNAME);

    XMLDirectMapping resumeMapping = new XMLDirectMapping();
    resumeMapping.setAttributeName("resume");
    resumeMapping.setXPath("resume/text()");
    XMLField resumeField = (XMLField) resumeMapping.getField();
    resumeField.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);