Skip Headers

# Mapping to a Union Field With an XML Direct Mapping

Previous

Next

Given the XML schema in Example: Schema for XML Direct Mapping to a Union Field, Figure: Java Class for XML Direct Mapping to a Union Field illustrates a Java class that can be mapped to a corresponding XML document. Note the `shoeSize` attribute in this class: when using a union field, the corresponding attribute must be able to store all possible values.

Schema for XML Direct Mapping to a Union Field

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

Java Class for XML Direct Mapping to a Union Field

Description of "Java Class for XML Direct Mapping to a Union Field"

Figure: XML Direct Mapping to the First Valid Union Type illustrates an XML direct mapping to a union field in an XML document that conforms to the schema in Example: Schema for XML Direct Mapping to a Union Field. When TopLink unmarshalls the XML document, it tries each of the union types until it can make a successful conversion. The first schema type in the union is `xsd:decimal`. Because "10.5" is a valid decimal, TopLink converts the value to the appropriate type. If the `Object` attribute is specific enough to trigger an appropriate value, TopLink will use that type instead. Otherwise, TopLink uses a default (in this case `BigDecimal`). You can override this behavior in Java code.

XML Direct Mapping to the First Valid Union Type

Description of "XML Direct Mapping to the First Valid Union Type"

Figure: XML Direct Mapping to Another Valid Union Type illustrates an XML direct mapping to union field in another XML document that conforms to the schema in Example: Schema for XML Direct Mapping to a Union Field. In this document, the value "M" is not a valid `xsd:decimal` type so the next union type is tried. The next union type is `xsd:string` and a conversion can be done.

XML Direct Mapping to Another Valid Union Type

Description of "XML Direct Mapping to Another Valid Union Type"

Example: Java for XML Direct Mapping to a Union Type shows how to configure this mapping in Java.

Java for XML Direct Mapping to a Union Type

    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);

To override the default conversion, use the `XMLUnionField` method `addConversion`:

    shoeSizeField.addConversion(XMLConstants.DECIMAL_QNAME, Float.class);