|
|
(8 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/simple_values002.htm |
− | |eclipselink=y
| + | |
− | |eclipselinktype=MOXy
| + | |
− | |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]
| + | |
− | * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlValue.html XmlValue]
| + | |
− | |toc=y
| + | |
− | }}
| + | |
− | =Mapping Collections of Simple Values=
| + | |
− | This section demonstrates several ways to map a collection of simple Java values directly to XML text nodes.
| + | |
− | | + | |
− | | + | |
− | ==Mapping to Text Nodes==
| + | |
− | | + | |
− | Given the XML schema in this example, the figure below illustrates the mapping of a Java collection to elements in a corresponding XML document.
| + | |
− | | + | |
− | <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="email-address" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
| + | |
− | </xsd:sequence>
| + | |
− | </xsd:complexType>
| + | |
− |
| + | |
− | </xsd:schema>
| + | |
− | </source>
| + | |
− | | + | |
− | [[Image:dc.png|XML Direct Collection Mapping to Text Nodes]]<br><br>
| + | |
− | | + | |
− | The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. All that is needed is the standard JAXB <tt>@XmlElement</tt> annotation.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | public class Customer {
| + | |
− | @XmlElement(name="email-address")
| + | |
− | private List<String> emailAddress;
| + | |
− | | + | |
− | public void setEmailAddress(List<String> value) {
| + | |
− | this.emailAddress = value;
| + | |
− | }
| + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | The example below shows how to to define your mapping information in EclipseLink's OXM metadata format.
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | ...
| + | |
− | <java-type name="Customer">
| + | |
− | <xml-root-element name="customer"/>
| + | |
− | <java-attributes>
| + | |
− | <xml-element java-attribute="emailAddresses" name="email-address"/>
| + | |
− | </java-attributes>
| + | |
− | </java-type>
| + | |
− | ...
| + | |
− | </source>
| + | |
− | | + | |
− | <br><br>
| + | |
− | | + | |
− | ==Mapping to Text Nodes with a Grouping Element==
| + | |
− | | + | |
− | Given the XML schema in this example, the figure below illustrates the mapping of a Java collection to elements in a corresponding XML document, using a grouping element to organize the elements of the collection.
| + | |
− | | + | |
− | <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="email-address" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
| + | |
− | </xsd:sequence>
| + | |
− | </xsd:complexType>
| + | |
− |
| + | |
− | </xsd:schema>
| + | |
− | </source>
| + | |
− | | + | |
− | [[Image:dcge.png|XML Direct Collection Mapping to Text Nodes with a Grouping Element]]<br><br>
| + | |
− | | + | |
− | The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. We specify the grouping element with the <tt>@XmlElementWrapper</tt> annotation.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | public class Customer {
| + | |
− | @XmlElement(name="email-address")
| + | |
− | @XmlElementWrapper(name="email-addresses")
| + | |
− | private List<String> emailAddresses;
| + | |
− | | + | |
− | public void setEmailAddress(List<String> value) {
| + | |
− | this.emailAddress = value;
| + | |
− | }
| + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | The example below shows how to to define your mapping information in EclipseLink's OXM metadata format.
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | ...
| + | |
− | <java-type name="Customer">
| + | |
− | <xml-root-element name="customer"/>
| + | |
− | <java-attributes>
| + | |
− | <xml-element java-attribute="emailAddresses" name="email-address">
| + | |
− | <xml-element-wrapper name="email-addresses"/>
| + | |
− | </xml-element>
| + | |
− | </java-attributes>
| + | |
− | </java-type>
| + | |
− | ...
| + | |
− | </source>
| + | |
− | | + | |
− | <br><br>
| + | |
− | | + | |
− | ==Mapping to a List Element==
| + | |
− | Given the XML schema in this exmaple, the figure below illustrates a mapping to an <tt>xsd:list</tt> type in a corresponding XML document. Using this mapping, you can represent the collection of simple Java objects as a <tt>String</tt> of white space delimited tokens in XML. Here, the <tt>tasks</tt> list contains three entries: "Design", "Code" and "Test".
| + | |
− | | + | |
− | <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>
| + | |
− | | + | |
− | [[Image:dcxmstn.gif|XMLDirect Mapping to a List Field]]<br><br>
| + | |
− | | + | |
− | The example below shows how to annotate your Java class to obtain this mapping with EclipseLink.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | public class Employee {
| + | |
− | @XmlList
| + | |
− | private List<String> tasks;
| + | |
− | | + | |
− | public void setTasks(List<String> value) {
| + | |
− | this.tasks = value;
| + | |
− | }
| + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | The example below shows how to to define your mapping information in EclipseLink's OXM metadata format.
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | ...
| + | |
− | <java-type name="Employee">
| + | |
− | <xml-root-element name="employee"/>
| + | |
− | <java-attributes>
| + | |
− | <xml-element java-attribute="tasks" xml-list="true"/>
| + | |
− | </java-attributes>
| + | |
− | </java-type>
| + | |
− | ...
| + | |
− | </source>
| + | |
− | | + | |
− | ==Mapping a Collection of XmlAttributes or XmlValues==
| + | |
− | | + | |
− | <tt>@XmlList</tt> can also be used in conjunction with <tt>@XmlAttribute</tt> or <tt>@XmlValue</tt>, as shown below. The collection will be represented as a space-separated string in the attribute.
| + | |
− | | + | |
− | '''''Java Annotations'''''
| + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | public class Customer {
| + | |
− | @XmlAttribute
| + | |
− | @XmlList
| + | |
− | public List<Integer> ids;
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlRootElement(name="phone-numbers")
| + | |
− | public class PhoneNumbers {
| + | |
− | @XmlValue
| + | |
− | @XmlList
| + | |
− | public List<String> numbers;
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | '''''EclipseLink OXM Metadata'''''
| + | |
− | <source lang="java">
| + | |
− | ...
| + | |
− | <java-type name="Customer">
| + | |
− | <xml-root-element name="customer"/>
| + | |
− | <java-attributes>
| + | |
− | <xml-attribute java-attribute="ids" xml-list="true"/>
| + | |
− | </java-attributes>
| + | |
− | </java-type>
| + | |
− | ...
| + | |
− | | + | |
− | ...
| + | |
− | <java-type name="PhoneNumbers">
| + | |
− | <xml-root-element name="phone-numbers"/>
| + | |
− | <java-attributes>
| + | |
− | <xml-value java-attribute="numbers" xml-list="true"/>
| + | |
− | </java-attributes>
| + | |
− | </java-type>
| + | |
− | ...
| + | |
− | </source>
| + | |
− | | + | |
− | '''''Example XML Documents'''''
| + | |
− | <source lang="xml">
| + | |
− | <customer ids="726 1982 1989 2991"/>
| + | |
− | </source>
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | <phone-numbers>6132883982 6139828817 18882982298</phone-numbers>
| + | |
− | </source>
| + | |
− | | + | |
− | | + | |
− | {{EclipseLink_MOXy
| + | |
− | |next= [[EclipseLink/UserGuide/MOXy/Simple Values/Enums|Enums]]
| + | |
− | |previous= [[EclipseLink/UserGuide/MOXy/Simple Values/Collections|Collections]]
| + | |
− | |up= [[EclipseLink/UserGuide/MOXy/Simple Values/Collections|Collections]]
| + | |
− | |version=2.2.0 DRAFT}}
| + | |