Loading
Developing JAXB Applications Using EclipseLink MOXy, Release 2.4
  Go To Table Of Contents
Contents
 Search
Other Searches
 PDF
PDF
Comments
Comments

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 Example 4-25, Figure 4-10 illustrates the mapping of a Java collection to elements in a corresponding XML document.

Example 4-25 Sample XML Schema

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

Figure 4-10 XML Direct Collection Mapping to Text Nodes

XML Direct Collection Mapping to Text Nodes
Description of "Figure 4-10 XML Direct Collection Mapping to Text Nodes"

Example 4-26 shows how to annotate your Java class to obtain this mapping with EclipseLink. All that is needed is the standard JAXB @XmlElement annotation.

Example 4-26 Using the @XmlElement Annotation

package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlElement(name="email-address")
   private List<String> emailAddress;
 
   ...
}
 

Example 4-27 shows how to define your mapping information in EclipseLink's OXM metadata format.

Example 4-27 Sample XML Mapping

...
<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 Example 4-28, Figure 4-11 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.

Example 4-28 Sample XML Schema

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

Figure 4-11 XML Direct Collection Mapping to Text Nodes with a Grouping Element

Description of Figure 4-11 follows
Description of "Figure 4-11 XML Direct Collection Mapping to Text Nodes with a Grouping Element"

Example 4-29 shows how to annotate your Java class to obtain this mapping with EclipseLink. We specify the grouping element with the @XmlElementWrapper annotation.

Example 4-29 Using the @XmlElementWrapper Annotation

package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlElement(name="email-address")
   @XmlElementWrapper(name="email-addresses")
   private List<String> emailAddresses;
 
   ...
}
 

Example 4-30 shows how to define your mapping information in EclipseLink's OXM metadata format.

Example 4-30 Sample XML Mapping

...
<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 Example 4-31, Figure 4-12 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.

Example 4-31 Sample XML Schema

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

Figure 4-12 XML Direct Mapping to a List Field

XMLDirect Mapping to a List Field
Description of "Figure 4-12 XML Direct Mapping to a List Field"

Example 4-32 shows how to annotate your Java class to obtain this mapping with EclipseLink.

Example 4-32 Using the @XmlList Annotation

package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
   @XmlList
   private List<String> tasks;
 
   ...
}
 

Example 4-33 shows how to define your mapping information in EclipseLink's OXM metadata format.

Example 4-33 Sample XML Mapping

...
<java-type name="Employee">
   <xml-root-element name="employee"/>
   <java-attributes>
      <xml-element java-attribute="tasks" xml-list="true"/>
   </java-attributes>
</java-type>
...
 

Mapping a Collection of XmlAttributes or XmlValues

@XmlList can also be used in conjunction with @XmlAttribute or @XmlValue, as shown in Example 4-34. The collection will be represented as a space-separated string in the attribute.

Example 4-34 Java Annotations

package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlAttribute
   @XmlList
   private List<Integer> ids;
 
   ...
}
 
package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement(name="phone-numbers")
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumbers {
   @XmlValue
   @XmlList
   private List<String> numbers;
 
   ...
}
 

Example 4-35 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 4-36 Example XML Documents

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