Class XMLDirectMapping

  • All Implemented Interfaces:
    Serializable, Cloneable, DirectMapping<AbstractSession,​AttributeAccessor,​ContainerPolicy,​Converter,​ClassDescriptor,​DatabaseField,​XMLMarshaller,​Session,​XMLUnmarshaller,​XMLRecord>, Mapping<AbstractSession,​AttributeAccessor,​ContainerPolicy,​ClassDescriptor,​DatabaseField,​XMLRecord>, XMLConverterMapping<XMLMarshaller,​Session,​XMLUnmarshaller>, MapComponentMapping, MapKeyMapping, XMLMapping, XMLNillableMapping
    Direct Known Subclasses:
    XMLBinaryDataMapping, XMLFragmentMapping

    public class XMLDirectMapping
    extends AbstractDirectMapping
    implements XMLMapping, DirectMapping<AbstractSession,​AttributeAccessor,​ContainerPolicy,​Converter,​ClassDescriptor,​DatabaseField,​XMLMarshaller,​Session,​XMLUnmarshaller,​XMLRecord>, XMLNillableMapping

    XML Direct Mappings map a Java attribute directly to XML attribute or text node.

    XML Direct Mappings can be used in the following scenarios:

    • Mapping to a Text Node
    • Mapping to an Attribute
    • Mapping to a Specified Schema Type
    • Mapping to a List Field
    • Mapping to a Union Field
    • Mapping to a Union of Lists
    • Mapping to a Union of Unions
    • Mapping with a Simple Type Translator

    Setting the XPath: TopLink XML mappings make use of XPath statements to find the relevant data in an XML document. The XPath statement is relative to the context node specified in the descriptor. The XPath may contain node type, path, and positional information. The XPath is specified on the mapping using the setXPath method.

    The following XPath statements may be used to specify the location of XML data relating to an object's name attribute:

    XPath statements
    XPath Description
    @name The "@" character indicates that the node is an attribute.
    text() "text()" indicates that the node is a text node. In this case the name value in the text node belongs to the context node.
    full-name/text() The name information is stored in the text node of the full-name element.
    personal-info/name/text() The XPath statement may be used to specify any valid path.
    name[2]/text() The XPath statement may contain positional information. In this case the name information is stored in the text node of the second occurrence of the name element.

    Mapping to a Specific Schema Type: 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, or a byte[] could be marshalled to a schema hexBinary or base64Binary node.

    XML Schema
    <?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>

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

    Mapping to a Union Field:

    XML Schema
    <?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="shoe-size" type="size-type"/>
        </xsd:sequence>
      </xsd:complexType>
      <xsd:simpleType name="size-type">
        <xsd:union memberTypes="xsd:decimal xsd:string"/>
      </xsd:simpleType>
    </xsd:schema>

    Code Sample
    XMLDirectMapping shoeSizeMapping = new XMLDirectMapping();
    shoeSizeMapping.setAttributeName("shoeSize");
    XMLUnionField shoeSizeField = new XMLUnionField();
    shoeSizeField.setXPath("shoe-size/text()");
    shoeSizeField.addSchemaType(XMLConstants.DECIMAL_QNAME);
    shoeSizeField.addSchemaType(XMLConstants.STRING_QNAME);
    shoeSizeMapping.setField(shoeSizeField);

    Preserving the Node Type: If the type of a node is not defined in your XML schema, you can configure an XML Direct Mapping to use the xsi:type attribute to provide type information.

    Code Sample
    XMLDirectMapping numberMapping = new XMLDirectMapping();
    numberMapping.setAttributeName("number");
    numberMapping.setXPath("number/text()");
    XMLField numberField = (XMLField) numberMapping.getField();
    numberField.setIsTypedTextField(true);

    More Information: For more information about using the XML Direct Mapping, see the "Understanding XML Mappings" chapter of the Oracle TopLink Developer's Guide.

    Since:
    Oracle TopLink 10g Release 2 (10.1.3)
    See Also:
    Serialized Form