Difference between revisions of "EclipseLink/UserGuide/MOXy/Simple Values/Collections/XMLDirectCollectionMapping"

From Eclipsepedia

Jump to: navigation, search
(Mapping to Text Nodes with a Grouping Element)
Line 57: Line 57:
 
...
 
...
 
</source>
 
</source>
 +
 +
<br><br>
  
 
==Mapping to Text Nodes with a Grouping Element==
 
==Mapping to Text Nodes with a Grouping Element==
Line 104: Line 106:
 
...
 
...
 
</source>
 
</source>
 +
 +
<br><br>
  
 
==Mapping to a List Element==
 
==Mapping to a List Element==

Revision as of 16:10, 21 December 2010


link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Contents

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.

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

XML Direct Collection Mapping to Text Nodes

The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. All that is needed is the standard JAXB @XmlElement annotation.

@XmlRootElement
public class Customer {
   @XmlElement(name="email-address")
   public List<String> emailAddress;
}

The example below shows how to to define your mapping information in EclipseLink's OXM metadata format.

...
<java-type name="Customer">
   <xml-root-element name="customer"/>
   <java-attributes>
      <xml-element java-attribute="emailAddresses" name="email-address"/>
   </java-attributes>
</java-type>
...



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.

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

XML Direct Collection Mapping to Text Nodes with a Grouping Element

The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. We specify the grouping element with the @XmlElementWrapper annotation.

@XmlRootElement
public class Customer {
   @XmlElement(name="email-address")
   @XmlElementWrapper(name="email-addresses")
   public List<String> emailAddresses;
}

The example below shows how to to define your mapping information in EclipseLink's OXM metadata format.

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



Mapping to a List Element

Given the XML schema in this exmaple, the figure below illustrates a mapping to an xsd:list type in a corresponding XML document. Using this mapping, you can represent the collection of simple Java objects as a String of white space delimited tokens in XML. Here, the tasks list contains three entries: "Design", "Code" and "Test".

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

XMLDirect Mapping to a List Field

The example below shows how to annotate your Java class to obtain this mapping with EclipseLink.

@XmlRootElement
public class Employee {
   @XmlList
   public List<String> tasks;
}

The example below shows how to to define your mapping information in EclipseLink's OXM metadata format.

...
<java-type name="Employee">
   <xml-root-element name="employee"/>
   <java-attributes>
      <xml-element java-attribute="tasks" xml-list="true"/>
   </java-attributes>
</java-type>
...
Idea.png
@XmlList can also be used in conjunction with @XmlAttribute and @XmlValue, as shown below.


Java Annotations

@XmlRootElement
public class Customer {
   @XmlAttribute
   @XmlList
   public List<Integer> ids;
}
@XmlRootElement(name="phone-numbers")
public class PhoneNumbers {
   @XmlValue
   @XmlList
   public List<String> numbers;
}

EclipseLink OXM Metadata

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

Example XML Documents

<customer ids="726 1982 1989 2991"/>
<phone-numbers>6132883982 6139828817 18882982298</phone-numbers>