Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Simple Values/Single Values/XMLDirectMapping"

m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/simple_values001.htm')
 
(83 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/simple_values001.htm
|info=y
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlAttribute.html XmlAttribute]
+
* [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlElement.html XmlElement]
+
|toc=y
+
}}
+
=XML Direct Mappings=
+
XML direct mappings map a Java attribute directly to XML text nodes. You can use an XML direct mapping in the following scenarios:
+
* [[#Mapping to an Attribute|Mapping to an Attribute]]
+
* [[#Mapping to a Text Node|Mapping to a Text Node]]
+
* [[#Mapping to a Specified Schema Type|Mapping to a Specified Schema Type]]
+
* [[#Mapping to a List Field with an XML Direct Mapping|Mapping to a List Field with an XML Direct Mapping]]
+
* [[#Mapping with a Simple Type Translator|Mapping with a Simple Type Translator]]
+
* [[#Mapping to a Union Field with an XML Direct Mapping|Mapping to a Union Field with an XML Direct Mapping]]
+
* [[#Mapping to a Union of Lists with an XML Direct Mapping|Mapping to a Union of Lists with an XML Direct Mapping]]
+
* [[#Mapping to a Union of Unions with an XML Direct Mapping|Mapping to a Union of Unions with an XML Direct Mapping]]
+
* [[#Mapping with a Simple Type Translator|Mapping with a Simple Type Translator]]
+
 
+
 
+
==Mapping to an Attribute==
+
Given the XML schema in this example, the [[#Figure 58-5|XML Direct Mapping to an Attribute]] figure illustrates an XML direct mapping to a text node by position in a corresponding XML document. The [[#Example 58-11|Java for XML Direct Mapping to an Attribute]] example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-10">
+
'''''Schema for XML Direct Mapping to an Attribute'''''
+
<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:attribute name="id" type="xsd:integer"/>
+
  </xsd:complexType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-5"></span>
+
'''''XML Direct Mapping to an Attribute'''''
+
 
+
[[Image:dxmatt.gif|XML Direct Mapping to an Attribute]]<br><br>
+
 
+
<span id="Example 58-11"></span>
+
'''''Java for XML Direct Mapping to an Attribute'''''
+
<source lang="java">
+
@XmlRootElement
+
public class Customer {
+
  @XmlAttribute
+
  public Integer id;
+
}
+
</source>
+
 
+
<span id="Example 58-11-2"></span>
+
'''''OXM for XML Direct Mapping to an Attribute'''''
+
<source lang="xml">
+
<java-type name="Customer">
+
  <xml-root-element name="customer"/>
+
  <java-attributes>
+
      <xml-attribute java-attribute="id" xml-path="@id"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
 
+
==Mapping to a Text Node==
+
This section describes using an XML direct mapping when doing the following:
+
* [[#Mapping to a Text Node in a Simple Sequence|Mapping to a Text Node in a Simple Sequence]]
+
* [[#Mapping to a Text Node in a Subelement|Mapping to a Text Node in a Subelement]]
+
* [[#Mapping to a Text Node by Position|Mapping to a Text Node by Position]]
+
* [[#Mapping to a Simple Text Node|Mapping to a Simple Text Node]]
+
 
+
 
+
===Mapping to a Text Node in a Simple Sequence===
+
Given the XML schema in the [[#Example 58-4|Schema for XML Direct Mapping to a Text Node in a Simple Sequence]] example, the [[#Figure 58-2|XML Direct Mapping to a Text Node in a Simple Sequence]] figure illustrates an XML direct mapping to individual text nodes in a sequence in a corresponding XML document. The [[#Example 58-5|Java for XML Direct Mapping to a Text Node in a Simple Sequence]] example shows how to configure this mapping in Java.
+
 
+
<span id="''Example 58-4"></span>
+
'''Schema for XML Direct Mapping to a Text Node in a Simple Sequence'''''
+
<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="first-name" type="xsd:string"/>
+
        <xsd:element name="last-name" type="xsd:string"/>
+
      </xsd:sequence>
+
  </xsd:complexType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-2"></span>
+
'''''XML Direct Mapping to a Text Node in a Simple Sequence'''''
+
 
+
[[Image:dxmss.gif|XML Direct Mapping to a Text Node in a Simple Sequence]]<br><br>
+
 
+
<span id="Example 58-5"></span>
+
'''''Java for XML Direct Mapping to a Text Node in a Simple Sequence'''''
+
<source lang="java">
+
@XmlRootElement
+
public class Customer {
+
  @XmlElement(name="first-name")
+
  public String firstName;
+
  @XmlElement(name="last-name")
+
  public String lastName;
+
}
+
</source>
+
 
+
<span id="Example 58-5-2"></span>
+
'''''OXM for XML Direct Mapping to a Text Node in a Simple Sequence'''''
+
<source lang="xml">
+
<java-type name="Customer">
+
  <xml-root-element name="customer"/>
+
  <java-attributes>
+
      <xml-element java-attribute="firstName" xml-path="first-name/text()"/>
+
      <xml-element java-attribute="lastName" xml-path="last-name/text()"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
 
+
===Mapping to a Text Node in a Subelement===
+
Given the XML schema in the [[#Example 58-6|Schema for XML Direct Mapping to a Text Node in a Subelement]] example, the [[#Figure 58-3|XML Direct Mapping to a Text Node in a Subelement]] figure illustrates an XML direct mapping to a text node in a subelement in a corresponding XML document. The [[#Example 58-7|Java for XML Direct Mapping to a Text Node in a Subelemen]] example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-6"></span>
+
'''''Schema for XML Direct Mapping to a Text Node in a Subelement'''''
+
<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="personal-info">
+
            <xsd:complexType>
+
              <xsd:sequence>
+
                  <xsd:element name="first-name" type="xsd:string"/>
+
                  <xsd:element name="last-name" type="xsd:string"/>
+
              <xsd:sequence>
+
            </xsd:complexType>
+
        </xsd:element>
+
      </xsd:sequence>
+
  </xsd:complexType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-3"></span>
+
'''''XML Direct Mapping to a Text Node in a Subelement'''''
+
 
+
[[Image:dxmse.gif|XML Direct Mapping to a Text Node in a Subelement]]<br><br>
+
 
+
<span id="Example 58-7"></span>
+
'''''Java for XML Direct Mapping to a Text Node in a Subelement'''''
+
<source lang="java">
+
@XmlRootElement
+
public class Customer {
+
  @XmlPath("personal-info/first-name/text()")
+
  public String firstName;
+
  @XmlPath("personal-info/last-name/text()")
+
  public String lastName;
+
}
+
</source>
+
 
+
<span id="Example 58-7-2"></span>
+
'''''OXM for XML Direct Mapping to a Text Node in a Subelement'''''
+
<source lang="xml">
+
<java-type name="Customer">
+
  <xml-root-element name="customer"/>
+
  <java-attributes>
+
      <xml-element java-attribute="firstName" xml-path="personal-info/first-name/text()"/>
+
      <xml-element java-attribute="lastName" xml-path="personal-info/last-name/text()"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
 
+
===Mapping to a Text Node by Position===
+
Given the XML schema in the [[#Example 58-8|Schema for XML Direct Mapping to Text Node by Position]] exampple, the [[#Figure 58-4|XML Direct Mapping to Text Node by Position]] figure illustrates an XML direct mapping to a text node by position in a corresponding XML document. The [[#Example 58-9|Java for XML Direct Mapping to Text Node by Position]] example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-8"></span>
+
''''Schema for XML Direct Mapping to Text Node by Position'''''
+
<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="name" type="xsd:string" maxOccurs="2"/>
+
      </xsd:sequence>
+
  </xsd:complexType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-4"></span>
+
'''''XML Direct Mapping to Text Node by Position'''''
+
 
+
[[Image:dxmpos.gif|XML Direct Mapping to Text Node by Position]]<br><br>
+
 
+
<span id="Example 58-9"></span>
+
'''''Java for XML Direct Mapping to Text Node by Position'''''
+
<source lang="java">
+
@XmlRootElement
+
public class Customer {
+
  @XmlPath("name[1]/text()")
+
  public String firstName;
+
  @XmlPath("name[2]/text()")
+
  public String lastName;
+
}
+
</source>
+
 
+
<span id="Example 58-9-2"></span>
+
'''''OXM for XML Direct Mapping to Text Node by Position'''''
+
<source lang="xml">
+
<java-type name="Customer">
+
  <xml-root-element name="customer"/>
+
  <java-attributes>
+
      <xml-element java-attribute="firstName" xml-path="name[1]/text()"/>
+
      <xml-element java-attribute="lastName" xml-path="name[2]/text()"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
 
+
===Mapping to a Simple Text Node===
+
Given the XML schema in the [[#Example 58-2|Schema for XML Direct Mapping to Simple Text Node]] example, the [[#Figure 58-1|XML Direct Mapping to Simple Text Node]] figure illustrates an XML direct mapping to a simple text node in a corresponding XML document. The [[#Example 58-3|Java for XML Direct Mapping to Simple Text Node]] example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-2"></span>
+
'''''Schema for XML Direct Mapping to Simple Text Node'''''
+
<source lang="xml">
+
<?xml version="1.0" encoding="UTF-8"?>
+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
 
+
  <xsd:element name="phone-number" type="xsd:string"/>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-1"></span>
+
'''''XML Direct Mapping to Simple Text Node'''''
+
 
+
[[Image:dxmstn.gif|XML Direct Mapping to Simple Text Node]]<br><br>
+
 
+
<span id="Example 58-3"></span>
+
'''''Java for XML Direct Mapping to Simple Text Node'''''
+
<source lang="java">
+
@XmlRootElement(name="phone-number")
+
public class PhoneNumber {
+
  @XmlValue
+
  public String number;
+
}
+
</source>
+
 
+
<span id="Example 58-3-2"></span>
+
'''''OXM for XML Direct Mapping to Simple Text Node'''''
+
<source lang="xml">
+
<java-type name="PhoneNumber">
+
  <xml-root-element name="phone-number"/>
+
  <java-attributes>
+
      <xml-value java-attribute="number"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
 
+
==Mapping to a Specified Schema Type==
+
In most cases, EclipseLink 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 EclipseLink should use. For example, a <tt>java.util.Calendar</tt> could be marshalled to a schema <tt>date</tt>, <tt>time</tt>, or <tt>dateTime</tt> node, or a <tt>byte[]</tt> could be marshalled to a schema <tt>hexBinary</tt> or <tt>base64Binary</tt> node.
+
 
+
Given the XML schema in this exmaple, the [[#Figure 58-6|XML Direct Mapping to a Specified Schema Type]] figure  illustrates an XML direct mapping to a text node by position in a corresponding XML document. The [[#Example 58-13|Java for XML Direct Mapping to a Specified Schema Type]] example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-12"></span>
+
'''''Schema for XML Direct Mapping to a Specified Schema Type'''''
+
<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="picture" type="xsd:hexBinary"/>
+
        <xsd:element name="resume" type="xsd:base64Binary"/>
+
      </xsd:sequence>
+
  </xsd:complexType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-6"></span>
+
'''''XML Direct Mapping to a Specified Schema Type'''''
+
 
+
[[Image:dxmscht.gif|XML Direct Mapping to a Specified Schema Type]]<br><br>
+
 
+
<span id="Example 58-13"></span>
+
'''''Java for XML Direct Mapping to a Specified Schema Type'''''
+
<source lang="java">
+
@XmlRootElement
+
public class Customer {
+
  @XmlElement
+
  @XmlSchemaType(name="hexBinary")
+
  @XmlJavaTypeAdapter(HexBinaryAdapter.class)
+
  public byte[] picture;
+
  @XmlElement
+
  public byte[] resume;
+
}
+
</source>
+
 
+
 
+
==Mapping to a List Field with an XML Direct Mapping==
+
Given the XML schema in this exmaple, the [[#Figure 58-7|XMLDirect Mapping to a List Field]] figure illustrates an XML direct mapping to an <tt>xsd:list</tt> type in a corresponding XML document when you represent the list in your object model as a <tt>String</tt> of white space delimited tokens. The [[#Example 58-15|Java for XML Direct Mapping to a List Field Node]] example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-14"></span>
+
'''''Schema for XML Direct Mapping to a List Field'''''
+
<source lang="xml">
+
<?xml version="1.0" encoding="UTF-8"?>
+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
 
+
  <xsd:element name="employee" type="employee-type"/>
+
 
+
  <xsd:complexType name="employee-type">
+
      <xsd:sequence>
+
        <xsd:element name="tasks" type="tasks-type"/>
+
      </xsd:sequence>
+
  </xsd:complexType>
+
 
+
  <xsd:simpleType name="tasks-type">
+
      <xsd:list itemType="xsd:string"/>
+
  </xsd:simpleType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
<span id="Figure 58-7"></span>
+
'''''XMLDirect Mapping to a List Field'''''
+
 
+
[[Image:dcxmstn.gif|XMLDirect Mapping to a List Field]]<br><br>
+
 
+
<span id="Example 58-15"></span>
+
'''''Java for XML Direct Mapping to a List Field Node'''''
+
<source lang="java">
+
@XmlRootElement
+
public class Employee {
+
  @XmlElement
+
  @XmlList
+
  public List<String> tasks;
+
}
+
</source>
+
 
+
<span id="Example 58-15-2"></span>
+
'''''OXM for XML Direct Mapping to a List Field Node'''''
+
<source lang="xml">
+
<java-type name="Employee">
+
  <xml-root-element name="employee"/>
+
  <java-attributes>
+
      <xml-element java-attribute="tasks" xml-path="tasks/text()" xml-list="true"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
 
+
==Mapping with a Simple Type Translator==
+
If the type of a node is not defined in your XML schema, you can configure an XML direct mapping to use the <tt>xsi:type</tt> attribute to provide type information.
+
 
+
Given the XML schema fragment in the [[#Example 58-22|Schema for XML Direct Mapping with Simple Type Translator]] example, the [[#Figure 58-13|Java Class for XML Direct Mapping with Simple Type Translator]] figure illustrates a Java class that can be mapped to a corresponding XML document.
+
 
+
<span id="Example 58-22"></span>
+
''''' Schema for XML Direct Mapping with Simple Type Translator'''''
+
<source lang="xml">
+
...
+
<xs:element name="area-code" type="anySimpleType"/>
+
<xs:element name="number" type="anySimpleType"/>
+
...
+
</source>
+
 
+
<span id="Figure 58-13"></span>
+
''''' Java Class for XML Direct Mapping with Simple Type Translator'''''
+
 
+
[[Image:dxmsttc.gif|Java Class for XML Direct Mapping with Simple Type Translator]]<br><br>
+
 
+
The following figure illustrates an XML direct mapping with a simple type translator in an XML document that conforms to the schema in the [[#Example 58-22|Schema for XML Direct Mapping with Simple Type Translator]] example.
+
 
+
<span id="Figure 58-14"></span>
+
''''' XML Direct Mapping with a Simple Type Translator'''''
+
 
+
[[Image:dxmsttm.gif|XML Direct Mapping with a Simple Type Translator]]<br><br>
+
 
+
This example shows how to configure this mapping in Java.
+
 
+
<span id="Example 58-23"></span>
+
'''''Java for XML Direct Mapping with Simple Type Translator'''''
+
<source lang="java">
+
@XmlRootElement(name="phone-number")
+
public class PhoneNumber {
+
  @XmlElement
+
  public Object areaCode;
+
  @XmlElement
+
  public Object number;
+
}
+
</source>
+
 
+
<span id="Example 58-23-2"></span>
+
'''''OXM for XML Direct Mapping with Simple Type Translator'''''
+
<source lang="xml">
+
<java-type name="PhoneNumber">
+
  <xml-root-element name="phone-number"/>
+
  <java-attributes>
+
      <xml-element java-attribute="areaCode"/>
+
      <xml-element java-attribute="number"/>
+
  </java-attributes>
+
</java-type>
+
</source>
+
 
+
For more information, see [[Introduction%20to%20Mappings%20(ELUG)#Simple Type Translator|Simple Type Translator]].
+
 
+
 
+
==Mapping to a Union Field with an XML Direct Mapping==
+
Given the XML schema in the [[#Example 58-16|Schema for XML Direct Mapping to a Union Field]] example, the [[#Figure 58-8|Java Class for XML Direct Mapping to a Union Field]] figure illustrates a Java class that can be mapped to 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.
+
 
+
<span id="Example 58-16"></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="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>
+
 
+
 
+
<span id="Figure 58-8"></span>
+
'''''Java Class for XML Direct Mapping to a Union Field'''''
+
 
+
[[Image:dxmuc.gif|Java Class for XML Direct Mapping to a Union Field]]<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|XML Direct Mapping to the First Valid Union Type]]<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|XML Direct Mapping to Another Valid Union Type]]<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|XML Direct Mapping to Union of Lists]]<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|Java Class for XML Direct Mapping to a Union of Unions]]<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/Single_Values|Single Values]]
+
|next=[[EclipseLink/UserGuide/MOXy/Simple_Values/Collections|Collections]]
+
|up=[[EclipseLink/UserGuide/MOXy/Simple_Values/Single_Values|Single Values]]
+
|version=2.2.0
+
}}
+

Latest revision as of 10:17, 8 November 2012

Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/simple_values001.htm

Back to the top