|
|
(26 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
− | {{EclipseLink_UserGuide
| + | Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/special_schema_types002.htm |
− | |info=y
| + | |
− | |toc=y
| + | |
− | |eclipselink=y
| + | |
− | |eclipselinktype=MOXy
| + | |
− | }}
| + | |
− | | + | |
− | =Mapping to a Union Field with an XML Direct Mapping=
| + | |
− | | + | |
− | Given the XML schema in this example, the figure below illustrates an XML direct mapping to an attribute in a corresponding XML document.
| + | |
− | | + | |
− | Note the <tt>shoeSize</tt> attribute in this class: when using a union field, the corresponding attribute must be able to store all possible values.
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | <?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>
| + | |
− | </source>
| + | |
− | | + | |
− | | + | |
− | <span id="Figure 58-8"></span>
| + | |
− | '''''Java Class for XML Direct Mapping to a Union Field'''''
| + | |
− | | + | |
− | [[Image:dxmuc.gif]]<br><br>
| + | |
− | | + | |
− | The [[#Figure 58-9| XML Direct Mapping to the First Valid Union Type]] figure illustrates an XML direct mapping to a union field in an XML document that conforms to the schema in the [[#Example 58-16|Schema for XML Direct Mapping to a Union Field]] example. When EclipseLink 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 <tt>xsd:decimal</tt>. Because "10.5" is a valid decimal, EclipseLink converts the value to the appropriate type. If the <tt>Object</tt> attribute is specific enough to trigger an appropriate value, EclipseLink will use that type instead. Otherwise, EclipseLink uses a default (in this case <tt>BigDecimal</tt>). You can override this behavior in Java code.
| + | |
− | | + | |
− | <span id="Figure 58-9"></span>
| + | |
− | '''''XML Direct Mapping to the First Valid Union Type'''''
| + | |
− | | + | |
− | [[Image:dxmuv.gif]]<br><br>
| + | |
− | | + | |
− | The [[#Figure 58-10|XML Direct Mapping to Another Valid Union Type]] figure illustrates an XML direct mapping to union field in another XML document that conforms to the schema in [[#Example 58-16|Schema for XML Direct Mapping to a Union Field]]. In this document, the value "M" is not a valid <tt>xsd:decimal</tt> type so the next union type is tried. The next union type is <tt>xsd:string</tt> and a conversion can be done.
| + | |
− | | + | |
− | <span id="Figure 58-10"></span>
| + | |
− | '''''XML Direct Mapping to Another Valid Union Type'''''
| + | |
− | | + | |
− | [[Image:dxmuvs.gif]]<br><br>
| + | |
− | | + | |
− | This example shows how to configure this mapping in Java.
| + | |
− | | + | |
− | <span id="Example 58-17"></span>
| + | |
− | '''''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 <tt>XMLUnionField</tt> method <tt>addConversion</tt><nowiki>:</nowiki>
| + | |
− | shoeSizeField.addConversion(XMLConstants.DECIMAL_QNAME, Float.class);
| + | |
− | | + | |
− | ==Mapping to a Union of Lists with an XML Direct Mapping==
| + | |
− | Given the XML schema in [[#Example 58-18|Schema for XML Direct Mapping to Union of Lists]], the [[#Figure 58-11|XML Direct Mapping to Union of Lists]] figure illustrates an XML direct mapping to a union of lists in a corresponding XML document. The [[#Example 58-19|Java for XML Direct Mapping to Union of Lists]] example shows how to configure this mapping in Java.
| + | |
− | | + | |
− | | + | |
− | <span id="Example 58-18"></span>
| + | |
− | ''''' Schema for XML Direct Mapping to Union of Lists'''''
| + | |
− | <?xml version="1.0" encoding="UTF-8"?>
| + | |
− | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
| + | |
− |
| + | |
− | <xsd:element name="vacation" type="unionOfLists"/>
| + | |
− | <xsd:simpleType name="unionOfLists">
| + | |
− | <xsd:union memberTypes="xsd:double">
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:list itemType="xsd:date"/>
| + | |
− | </xsd:simpleType>
| + | |
− |
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:list itemType="xsd:integer"/>
| + | |
− | </xsd:simpleType>
| + | |
− | </xsd:union>
| + | |
− | </xsd:simpleType>
| + | |
− | </xsd:schema>
| + | |
− |
| + | |
− | | + | |
− | | + | |
− | <span id="'Figure 58-11"></span>
| + | |
− | ''''' XML Direct Mapping to Union of Lists'''''
| + | |
− | | + | |
− | [[Image:dxuofl.gif]]<br><br>
| + | |
− | | + | |
− | Note that in this example, valid XML documents contain either all <tt>xsd:double</tt>, all <tt>xsd:date</tt>, or all <tt>xsd:integer</tt> values.
| + | |
− | | + | |
− | | + | |
− | <span id="Example 58-19"></span>
| + | |
− | ''''' Java for XML Direct Mapping to Union of Lists'''''
| + | |
− | XMLDirectMapping mapping = new XMLDirectMapping();
| + | |
− | mapping.setAttributeName("vacation");
| + | |
− | mapping.setXPath("UnionOfLists/text()");
| + | |
− | | + | |
− | ==Mapping to a Union of Unions with an XML Direct Mapping==
| + | |
− | Given the XML schema in the [[#Example 58-20|Schema for XML Direct Mapping to a Union of Unions]] example, the [[#Figure 58-12|Java Class for XML Direct Mapping to a Union of Unions]] figure illustrates a Java class that can be mapped to a corresponding XML document. The [[#Example 58-21|Java for XML Direct Mapping to a Union of Unions]] example shows how to configure this mapping in Java.
| + | |
− | | + | |
− | | + | |
− | <span id="Example 58-20"></span>
| + | |
− | ''''' Schema for XML Direct Mapping to a Union of Unions'''''
| + | |
− | <?xml version="1.0" encoding="UTF-8"?>
| + | |
− | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
| + | |
− | <xsd:element name="vacation" type="unionOfUnions"/>
| + | |
− | <xsd:simpleType name="unionOfUnions">
| + | |
− | <xsd:union>
| + | |
− |
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:union>
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:list itemType="xsd:date"/>
| + | |
− | </xsd:simpleType>
| + | |
− | <xsd:simpleType>
| + | |
− |
| + | |
− | <xsd:list itemType="xsd:integer"/>
| + | |
− | </xsd:simpleType>
| + | |
− | </xsd:union>
| + | |
− | </xsd:simpleType>
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:union>
| + | |
− |
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:list itemType="xsd:string"/>
| + | |
− | </xsd:simpleType>
| + | |
− | <xsd:simpleType>
| + | |
− | <xsd:list itemType="xsd:float"/>
| + | |
− | </xsd:simpleType>
| + | |
− |
| + | |
− | </xsd:union>
| + | |
− | </xsd:simpleType>
| + | |
− | </xsd:union>
| + | |
− | </xsd:simpleType>
| + | |
− | </xsd:schema>
| + | |
− | | + | |
− | | + | |
− | <span id="Figure 58-12"></span>
| + | |
− | ''''' Java Class for XML Direct Mapping to a Union of Unions'''''
| + | |
− | | + | |
− | [[Image:dxuofu.gif]]<br><br>
| + | |
− | | + | |
− | | + | |
− | <span id="Example 58-21"></span>
| + | |
− | ''''' Java for XML Direct Mapping to a Union of Unions'''''
| + | |
− | XMLDirectMapping vacationMapping = new XMLDirectMapping();
| + | |
− | vacationMapping.setAttributeName("vacation");
| + | |
− | XMLUnionField vacationField = new XMLUnionField();
| + | |
− | vacationField.setXPath("vacation/text()");
| + | |
− | vacationField.addSchemaType(XMLConstants.DATE_QNAME);
| + | |
− | vacationField.addSchemaType(XMLConstants.INTEGER_QNAME);
| + | |
− | vacationField.addSchemaType(XMLConstants.STRING_QNAME);
| + | |
− | vacationField.addSchemaType(XMLConstants.FLOAT_QNAME);
| + | |
− | vacationMapping.setField(vacationField);
| + | |
− | | + | |
− | | + | |
− | | + | |
− | {{EclipseLink_MOXy
| + | |
− | |previous= [[EclipseLink/UserGuide/MOXy/Simple_Values/Special_Schema_Types|Special Schema Types]]
| + | |
− | |up= [[EclipseLink/UserGuide/MOXy/Simple_Values/Special_Schema_Types|Special Schema Types]]
| + | |
− | |next= [[EclipseLink/UserGuide/MOXy/Simple_Values/Binary_Types|Binary Types]]
| + | |
− | |version=2.2.0 Draft
| + | |
− | }}
| + | |