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.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Simple Values/Collections/XMLDirectCollectionMapping"
Line 18: | Line 18: | ||
Given the XML schema in this example, the figure below illustrates an XML direct collection mapping to elements in a corresponding XML document. | Given the XML schema in this example, the figure below illustrates an XML direct collection mapping to elements in a corresponding XML document. | ||
− | |||
<source lang="xml"> | <source lang="xml"> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
Line 33: | Line 32: | ||
</xsd:schema> | </xsd:schema> | ||
</source> | </source> | ||
− | |||
− | |||
[[Image:dc.png|XML Direct Collection Mapping to Text Nodes]]<br><br> | [[Image:dc.png|XML Direct Collection Mapping to Text Nodes]]<br><br> | ||
Line 40: | Line 37: | ||
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. | 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"> | <source lang="java"> | ||
@XmlRootElement | @XmlRootElement | ||
Line 51: | Line 47: | ||
The example below shows how to to define your mapping information in EclipseLink's OXM metadata format. | The example below shows how to to define your mapping information in EclipseLink's OXM metadata format. | ||
− | |||
<source lang="xml"> | <source lang="xml"> | ||
... | ... | ||
Line 67: | Line 62: | ||
Given the XML schema in this example, the figure below illustrates an XML direct collection mapping to elements in a corresponding XML document, using a grouping element to contain the elements of the collection. | Given the XML schema in this example, the figure below illustrates an XML direct collection mapping to elements in a corresponding XML document, using a grouping element to contain the elements of the collection. | ||
− | |||
<source lang="xml"> | <source lang="xml"> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
Line 82: | Line 76: | ||
</xsd:schema> | </xsd:schema> | ||
</source> | </source> | ||
− | |||
− | |||
[[Image:dcge.png|XML Direct Collection Mapping to Text Nodes with a Grouping Element]]<br><br> | [[Image:dcge.png|XML Direct Collection Mapping to Text Nodes with a Grouping Element]]<br><br> | ||
Line 89: | Line 81: | ||
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. | 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"> | <source lang="java"> | ||
@XmlRootElement | @XmlRootElement | ||
Line 100: | Line 91: | ||
The example below shows how to to define your mapping information in EclipseLink's OXM metadata format. | The example below shows how to to define your mapping information in EclipseLink's OXM metadata format. | ||
− | |||
<source lang="xml"> | <source lang="xml"> | ||
... | ... | ||
Line 116: | Line 106: | ||
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. | 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. | ||
− | |||
<source lang="xml"> | <source lang="xml"> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
Line 135: | Line 124: | ||
</xsd:schema> | </xsd:schema> | ||
</source> | </source> | ||
− | |||
− | |||
[[Image:dcxmstn.gif|XMLDirect Mapping to a List Field]]<br><br> | [[Image:dcxmstn.gif|XMLDirect Mapping to a List Field]]<br><br> | ||
Line 142: | Line 129: | ||
The example below shows how to annotate your Java class to obtain this mapping with EclipseLink. | The example below shows how to annotate your Java class to obtain this mapping with EclipseLink. | ||
− | |||
<source lang="java"> | <source lang="java"> | ||
@XmlRootElement | @XmlRootElement | ||
Line 153: | Line 139: | ||
The example below shows how to to define your mapping information in EclipseLink's OXM metadata format. | The example below shows how to to define your mapping information in EclipseLink's OXM metadata format. | ||
− | |||
<source lang="xml"> | <source lang="xml"> | ||
... | ... |
Revision as of 11:54, 21 December 2010
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Contents
XML Direct Collection Mappings
XML direct collection mappings map a collection of simple Java values directly to XML text nodes. You can use an XML direct collection mapping in the following scenarios:
Mapping to Text Nodes
Given the XML schema in this example, the figure below illustrates an XML direct collection mapping 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>
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 an XML direct collection mapping to elements in a corresponding XML document, using a grouping element to contain 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>
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 a List Element
Given the XML schema in this exmaple, the XMLDirect Mapping to a List Field figure illustrates an XML direct mapping to an xsd:list type in a corresponding XML document when you represent the list in your object model as a String of white space delimited tokens.
<?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>
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-path="tasks/text()" xml-list="true"/> </java-attributes> </java-type> ...
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>