blob: 7e0fbecf2c48e6ae02afc865eb63676858c238d7 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>Mapping Simple Java Values to XML Text Nodes | EclipseLink 2.7 Understanding EclipseLink</title>
<meta name="generator" content="Oracle DARB XHTML Converter (Mode = document) - Version 1.0.22 Build 1" />
<meta name="date" content="2014-06-10T10:35:15Z" />
<meta name="robots" content="noarchive" />
<meta name="doctitle" content="Mapping Simple Java Values to XML Text Nodes" />
<meta name="relnum" content="Release 2.7" />
<link rel="stylesheet" type="text/css" href="../../dcommon/style.css" media="screen" />
<link rel="copyright" href="../../dcommon/html/cpyr.htm" title="Copyright" type="text/html" />
<link rel="start" href="../../index.htm" title="Home" type="text/html" />
<link rel="contents" href="toc.htm" title="Contents" type="text/html" />
<link rel="prev" href="jpatoxml002.htm" title="Previous" type="text/html" />
<link rel="next" href="jpatoxml004.htm" title="Next" type="text/html" />
<!-- START: Disqus --><script type="text/javascript"> var disqus_developer = 0; </script><!-- END: Disqus --><!-- START: Sharethis --><script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script><script type="text/javascript" src="http://s.sharethis.com/loader.js"></script> <!-- END: Sharethis --></head>
<body bgcolor="#FFFFFF"><iframe id="docheader" frameborder="0" framemargin="0" scrolling="no" src="../../dcommon/header.html"></iframe><script src="http://www.google.com/jsapi" type="text/javascript"></script><script type="text/javascript"> google.load('search', '1', {language : 'en'}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var googleAnalyticsOptions = {}; googleAnalyticsOptions['queryParameter'] = 'q'; googleAnalyticsOptions['categoryParameter'] = ''; customSearchOptions['googleAnalyticsOptions'] = googleAnalyticsOptions; var customSearchControl = new google.search.CustomSearchControl( '016171230611334810008:enkkfjmsqju', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); var options = new google.search.DrawOptions(); options.setSearchFormRoot('cse-search-form'); customSearchControl.draw('cse', options); }, true);</script><link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" /><div id="cse" style="width:100%;"></div>
<div class="header"><a id="top" name="top"></a>
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td align="left" valign="top"><font face="helvetica, arial, sans-serif"><b>EclipseLink Solutions Guide for EclipseLink</b><br />
Release 2.7</div></td>
<td valign="bottom" align="right" width="144">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>&nbsp;</td>
<td align="center" valign="top"><a href="toc.htm"><img src="../../dcommon/images/contents.png" alt="Go To Table Of Contents" border="0" height="16" width="16" /><br />
</td><td>&nbsp;</td><td align="center"><a href="../../" target="_top" class="external text" title="Search" rel="nofollow"><img src="../../dcommon/images/search.png" alt="Search" style="border:0;" /><br /><span class="mini"></span></a></td><td>&nbsp;</td><td align="center"><a href="../eclipselink_otlcg.pdf" title="PDF" target="_blank"><img src="../../dcommon/images/pdf_icon.png" style="padding-right:5px;border:0" alt="PDF"></a></td>
</tr>
</table>
</td>
</tr>
</table>
<hr />
<table class="navigation simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100" align="center">
<tr>
<td align="center"><a href="jpatoxml002.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td align="center"><a href="jpatoxml004.htm"><img src="../../dcommon/images/rarrow.png" alt="Next" border="0" height="16" width="16" /></a></td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<!-- class="header" -->
<div class="ind"><!-- End Header --><a id="BEIEJGCE" name="BEIEJGCE"></a><a id="TLADG324" name="TLADG324"></a>
<div class="sect1"><!-- infolevel="all" infotype="General" -->
<h1 class="sect1"><font face="arial, helvetica, sans-serif" color="#330099">Mapping Simple Java Values to XML Text Nodes</font></h1>
<p>This section demonstrates several ways to map simple Java values directly to XML text nodes. It includes the following examples:</p>
<ul>
<li>
<p><a href="#BEIJIEGC">Mapping a Value to an Attribute</a></p>
</li>
<li>
<p><a href="#BEIDFEIC">Mapping a Value to a Text Node</a></p>
</li>
</ul>
<a id="BEIJIEGC" name="BEIJIEGC"></a><a id="TLADG325" name="TLADG325"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" -->
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping a Value to an Attribute</font></h2>
<p>This example maps the <code>id</code> property in the Java object <code>Customer</code> to its XML representation as an attribute of the <code>&lt;customer&gt;</code> element. The XML will be based on the schema in <a href="#BEICBDBC">Example 15-10</a>.</p>
<div class="example"><a id="BEICBDBC" name="BEICBDBC"></a><a id="TLADG326" name="TLADG326"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-10 Example XML Schema</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;xsd:element name="customer" type="customer-type"/&gt;
&lt;xsd:complexType name="customer-type"&gt;
&lt;xsd:attribute name="id" type="xsd:integer"/&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<p>The following procedures demonstrate how to map the <code>id</code> property from the Java object and, alternately, how to represent the value in EclipseLink's Object-to-XML Mapping (OXM) metadata format.</p>
<a id="TLADG327" name="TLADG327"></a>
<div class="sect3"><!-- infolevel="all" infotype="General" --><a id="sthref108" name="sthref108"></a>
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping from the Java Object</font></h3>
<p>The key to creating this mapping from a Java object is the <code>@XmlAttribute</code> JAXB annotation, which maps the field to the XML attribute. To create this mapping:</p>
<ol>
<li>
<p>Create the object and import <code>javax.xml.bind.annotation.*</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
</pre></li>
<li>
<p>Declare the <code>Customer</code> class and use the <code>@XmlRootElement</code> annotation to make it the root element. Set the XML accessor type to <code>FIELD</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlRootElement</strong>
@XmlAccessorType(XmlAccessType.FIELD)
<strong>public class Customer {</strong>
</pre></li>
<li>
<p>Map the <code>id</code> property in the <code>Customer</code> class as an attribute:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlAttribute</strong>
private Integer id;
</pre></li>
</ol>
<p>The object should look like <a href="#BEIHFEHF">Example 15-11</a>.</p>
<div class="example"><a id="BEIHFEHF" name="BEIHFEHF"></a><a id="TLADG328" name="TLADG328"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-11 Customer Object with Mapped id Property</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlAttribute
private Integer id;
...
}
</pre></div>
<!-- class="example" --></div>
<!-- class="sect3" -->
<a id="TLADG329" name="TLADG329"></a>
<div class="sect3"><!-- infolevel="all" infotype="General" --><a id="sthref109" name="sthref109"></a>
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Defining the Mapping in OXM Metadata Format</font></h3>
<p>If you want to represent the mapping in EclipseLink's OXM metadata format, you need to use the XML tags defined in the <code>eclipselink-oxm.xml</code> file and populate them with the appropriate values, as shown in <a href="#BEICFEDE">Example 15-12</a>.</p>
<div class="example"><a id="BEICFEDE" name="BEICFEDE"></a><a id="TLADG330" name="TLADG330"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-12 Mapping id as an Attribute in OXM Metadata Format</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name=<strong>"Customer"</strong>&gt;
&lt;xml-root-element name=<strong>"customer"</strong>/&gt;
&lt;java-attributes&gt;
&lt;xml-attribute java-attribute=<strong>"id"</strong>/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" -->
<p>For more information about the OXM metadata format, see <a href="jpatoxml004.htm#BEIJGBGA">Using XML Metadata Representation to Override JAXB Annotations</a>.</p>
</div>
<!-- class="sect3" --></div>
<!-- class="sect2" -->
<a id="BEIDFEIC" name="BEIDFEIC"></a><a id="TLADG331" name="TLADG331"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" -->
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping a Value to a Text Node</font></h2>
<p>EclipseLink makes it easy for you to map values from a Java object to various kinds of XML text nodes; for example, to simple text nodes, text nodes in a simple sequence, in a subset, or by position. These mappings are demonstrated in the following examples:</p>
<ul>
<li>
<p><a href="#BEIFAICJ">Mapping a Value to a Simple Text Node</a></p>
</li>
<li>
<p><a href="#BEIBEFEJ">Mapping Values to a Text Node in a Simple Sequence</a></p>
</li>
<li>
<p><a href="#BEICIFCI">Mapping a Value to a Text Node in a Sub-element</a></p>
</li>
<li>
<p><a href="#BEIDCEHD">Mapping Values to a Text Node by Position</a></p>
</li>
</ul>
<a id="BEIFAICJ" name="BEIFAICJ"></a><a id="TLADG332" name="TLADG332"></a>
<div class="sect3"><!-- infolevel="all" infotype="General" -->
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping a Value to a Simple Text Node</font></h3>
<p>You can map a value from a Java object either by using JAXB annotations in the Java object or, alternately, by representing the mapping in EclipseLink's OXM metadata format.</p>
<a id="TLADG333" name="TLADG333"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref110" name="sthref110"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Mapping by Using JAXB Annotations</font></h4>
<p>Assuming the associated schema defines an element called <code>&lt;phone-number&gt;</code> which accepts a string value, you can use the <code>@XmlValue</code> annotation to map a string to the <code>&lt;phone-number&gt;</code> node. Do the following:</p>
<ol>
<li>
<p>Create the object and import <code>javax.xml.bind.annotation.*</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
</pre></li>
<li>
<p>Declare the <code>PhoneNumber</code> class and use the <code>@XmlRootElement</code> annotation to make it the root element with the name phone-number. Set the XML accessor type to <code>FIELD</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlRootElement(name="phone-number")</strong>
@XmlAccessorType(XmlAccessType.FIELD)
<strong>public class PhoneNumber {</strong>
</pre></li>
<li>
<p>Insert the <code>@XmlValue</code> annotation on the line before the <code>number</code> property in the Customer class to map this value as an attribute:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlValue</strong>
private String number;
</pre></li>
</ol>
<p>The object should look like <a href="#BEIGFCJG">Example 15-13</a>.</p>
<div class="example"><a id="BEIGFCJG" name="BEIGFCJG"></a><a id="TLADG334" name="TLADG334"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-13 PhoneNumber Object with Mapped number Property</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="phone-number")
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
@XmlValue
private String number;
...
}
</pre></div>
<!-- class="example" --></div>
<!-- class="sect4" -->
<a id="TLADG335" name="TLADG335"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref111" name="sthref111"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Defining the Mapping in OXM Metadata Format</font></h4>
<p>If you want to represent the mapping in EclipseLink's OXM metadata format, you need to use the XML tags defined in the <code>eclipselink-oxm.xml</code> file and populate them with the appropriate values, as shown in <a href="#BEIECBDG">Example 15-14</a>.</p>
<div class="example"><a id="BEIECBDG" name="BEIECBDG"></a><a id="TLADG336" name="TLADG336"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-14 Mapping number as an Attribute in OXM Metadata Format</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name=<strong>"PhoneNumber"</strong>&gt;
&lt;xml-root-element name=<strong>"phone-number"</strong>/&gt;
&lt;java-attributes&gt;
&lt;xml-value java-attribute=<strong>"number"</strong>/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect4" --></div>
<!-- class="sect3" -->
<a id="BEIBEFEJ" name="BEIBEFEJ"></a><a id="TLADG337" name="TLADG337"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping Values to a Text Node in a Simple Sequence</font></h3>
<p>You can map a sequence of values, for example a customer's first and last name, as separate elements either by using JAXB annotations or by representing the mapping in EclipseLink's OXM metadata format. The following procedures illustrate how to map values for a customers' first names and last names</p>
<a id="TLADG338" name="TLADG338"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref112" name="sthref112"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Mapping by Using JAXB Annotations</font></h4>
<p>Assuming the associated schema defines the following elements:</p>
<ul>
<li>
<p><code>&lt;customer&gt;</code> of the type customer-type, which itself is defined as a <code>complexType</code>.</p>
</li>
<li>
<p>Sequential elements called <code>&lt;first-name&gt;</code> and <code>&lt;last-name&gt;</code>, both of the type <code>string</code>.</p>
</li>
</ul>
<p>you can use the <code>@XmlElement</code> annotation to map values for a customer's first and last name to the appropriate XML nodes. To do so:</p>
<ol>
<li>
<p>Create the object and import <code>javax.xml.bind.annotation.*</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
</pre></li>
<li>
<p>Declare the <code>Customer</code> class and use the <code>@XmlRootElement</code> annotation to make it the root element. Set the XML accessor type to <code>FIELD</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlRootElement</strong>
@XmlAccessorType(XmlAccessType.FIELD)
<strong>public class Customer {</strong>
</pre></li>
<li>
<p>Define the <code>firstname</code> and <code>lastname</code> properties and annotate them with the <code>@XmlElement</code> annotation. Use the <code>name=</code> argument to customize the XML element name (if you do not explicitly set the name with <code>name=</code>, the XML element will match the Java attribute name; for example, here the <code>&lt;first-name&gt;</code> element combination would be specified <code>&lt;firstName&gt; &lt;/firstName&gt;</code> in XML).</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlElement(name="first-name")</strong>
private String firstName;
<strong>@XmlElement(name="last-name")</strong>
private String lastName;
</pre></li>
</ol>
<p>The object should look like <a href="#BEIFIJIH">Example 15-15</a>.</p>
<div class="example"><a id="BEIFIJIH" name="BEIFIJIH"></a><a id="TLADG339" name="TLADG339"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-15 Customer Object Mapping Values to a Simple Sequence</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name="first-name")
private String firstName;
@XmlElement(name="last-name")
private String lastName;
...
}
</pre></div>
<!-- class="example" --></div>
<!-- class="sect4" -->
<a id="TLADG340" name="TLADG340"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref113" name="sthref113"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Defining the Mapping in OXM Metadata Format</font></h4>
<p>If you want to represent the mapping in EclipseLink's OXM metadata format, you need to use the XML tags defined in the <code>eclipselink-oxm.xml</code> file and populate them with the appropriate values, as shown in <a href="#BEIIHFFE">Example 15-16</a>.</p>
<div class="example"><a id="BEIIHFFE" name="BEIIHFFE"></a><a id="TLADG341" name="TLADG341"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-16 Mapping Sequential Attributes in OXM Metadata Format</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name=<strong>"Customer"</strong>&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute=<strong>"firstName" name="first-name"</strong>/&gt;
&lt;xml-element java-attribute=<strong>"lastName" name="last-name"</strong>/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect4" --></div>
<!-- class="sect3" -->
<a id="BEICIFCI" name="BEICIFCI"></a><a id="TLADG342" name="TLADG342"></a>
<div class="sect3"><!-- infolevel="all" infotype="General" -->
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping a Value to a Text Node in a Sub-element</font></h3>
<p>You can map values from a Java object to text nodes that are nested as a subelement in the XML document by using JAXB annotations or by representing the mapping in EclipseLink's OXM metadata format. For example, if you want to populate <code>&lt;first-name&gt;</code> and <code>&lt;last-name&gt;</code> elements, which are sub-elements of a <code>&lt;personal-info&gt;</code> element under a <code>&lt;customer&gt;</code> root, you could use the following procedures to achieve these mappings.</p>
<a id="TLADG343" name="TLADG343"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref114" name="sthref114"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Mapping by Using JAXB Annotations</font></h4>
<p>Assuming the associated schema defines the following elements:</p>
<ul>
<li>
<p><code>&lt;customer&gt;</code> of the type customer-type, which itself is defined as a complexTpe.</p>
</li>
<li>
<p><code>&lt;personal-info&gt;</code></p>
</li>
<li>
<p>Sub-elements of <code>&lt;personal-info&gt;</code> called <code>&lt;first-name&gt;</code> and <code>&lt;last-name&gt;</code>, both of the type string</p>
</li>
</ul>
<p>you can use JAXB annotations to map values for a customer's first and last name to the appropriate XML sub-element nodes. Because this example goes beyond a simple element name customization and actually introduces new XML structure, it uses EclipseLink's <code>@XmlPath</code> annotation. To achieve this mapping:</p>
<ol>
<li>
<p>Create the object and import <code>javax.xml.bind.annotation.*</code> and <code>org.eclipse.persistence.oxm.annotations.*</code>.</p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;
</pre></li>
<li>
<p>Declare the <code>Customer</code> class and use the <code>@XmlRootElement</code> annotation to make it the root element. Set the XML accessor type to <code>FIELD</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlRootElement</strong>
@XmlAccessorType(XmlAccessType.FIELD)
<strong>public class Customer</strong> {
</pre></li>
<li>
<p>Define the <code>firstName</code> and <code>lastName</code> properties.</p>
</li>
<li>
<p>Map the <code>firstName</code> and <code>lastName</code> properties to the sub-elements defined by the XML schema by inserting the <code>@XmlPath</code> annotation on the line immediately preceding the property declaration. For each annotation, define the mapping by specifying the appropriate XPath predicate:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlPath("personal-info/first-name/text()")</strong>
private String firstName;
<strong>@XmlPath("personal-info/last-name/text()")</strong>
private String lastName;
</pre></li>
</ol>
<p>The object should look like <a href="#BEIBCIDB">Example 15-17</a>.</p>
<div class="example"><a id="BEIBCIDB" name="BEIBCIDB"></a><a id="TLADG344" name="TLADG344"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-17 Customer Object Mapping Properties to Sub-elements</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlPath("personal-info/first-name/text()")
private String firstName;
@XmlPath("personal-info/last-name/text()")
private String lastName;
...
}
</pre></div>
<!-- class="example" --></div>
<!-- class="sect4" -->
<a id="TLADG345" name="TLADG345"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref115" name="sthref115"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Defining the Mapping in OXM Metadata Format</font></h4>
<p>If you want to represent the mapping in EclipseLink's OXM metadata format, you need to use the XML tags defined in the <code>eclipselink-oxm.xml</code> file and populate them with the appropriate values, as shown in <a href="#BEIEADBI">Example 15-18</a>.</p>
<div class="example"><a id="BEIEADBI" name="BEIEADBI"></a><a id="TLADG346" name="TLADG346"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-18 Mapping Attributes as Sub-elements in OXM Metadata Format</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name=<strong>"customer"</strong>/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="<strong>firstName</strong>" xml-path=<strong>"personal-info/first-name/text()"</strong>/&gt;
&lt;xml-element java-attribute=<strong>"lastName"</strong> xml-path="<strong>personal-info/last-name/text()"</strong>/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect4" --></div>
<!-- class="sect3" -->
<a id="BEIDCEHD" name="BEIDCEHD"></a><a id="TLADG347" name="TLADG347"></a>
<div class="sect3"><!-- infolevel="all" infotype="General" -->
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping Values to a Text Node by Position</font></h3>
<p>When multiple nodes have the same name, map their values from the Java object by specifying their position in the XML document. Do this by using mapping the values to the <em>position</em> of the attribute rather than the attribute's name. You can do this either by using JAXB annotations or by or by representing the mapping in EclipseLink's OXM metadata format. In the following example, XML contains two <code>&lt;name&gt;</code> elements; the first occurrence of name should represent the Customer's first name, the second name their last name.</p>
<a id="TLADG348" name="TLADG348"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref116" name="sthref116"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Mapping by Using JAXB Annotations</font></h4>
<p>Assuming an XML schema that defines the following attributes:</p>
<ul>
<li>
<p><code>&lt;customer&gt;</code> of the type customer-type, which itself is specified as a <code>complexType</code></p>
</li>
<li>
<p><code>&lt;name&gt;</code> of the type <code>String</code></p>
</li>
</ul>
<p>this example again uses the JAXB <code>@XmlPath</code> annotation to map a customer's first and last names to the appropriate <code>&lt;name&gt;</code> element. It also uses the <code>@XmlType(propOrder)</code> annotation to ensure that the elements are always in the proper positions. To achieve this mapping:</p>
<ol>
<li>
<p>Create the object and import <code>javax.xml.bind.annotation.*</code> and <code>org.eclipse.persistence.oxm.annotations.XmlPath</code>.</p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
</pre></li>
<li>
<p>Declare the <code>Customer</code> class and insert the <code>@XmlType(propOrder)</code> annotation with the arguments <code>"firstName"</code> followed by <code>"lastName"</code>. Insert the <code>@XmlRootElement</code> annotation to make <code>Customer</code> the root element and set the XML accessor type to <code>FIELD</code>:</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlRootElement</strong>
<strong>@XmlType(propOrder={"firstName", "lastName"})</strong>
@XmlAccessorType(XmlAccessType.FIELD)
<strong>public class Customer</strong> {
</pre></li>
<li>
<p>Define the properties <code>firstName</code> and <code>lastName</code> with the type <code>String</code>.</p>
</li>
<li>
<p>Map the properties <code>firstName</code> and <code>lastName</code> to the appropriate position in the XML document by inserting the <code>@XmlPath</code> annotation with the appropriate XPath predicates.</p>
<pre xml:space="preserve" class="oac_no_warn">
<strong>@XmlPath("name[1]/text()")</strong>
private String firstName;
<strong>@XmlPath("name[2]/text()")</strong>
private String lastName;
</pre>
<p>The predicates, <code>"name[1]/text()"</code> and <code>"name[2]/text()"</code> indicate the <code>&lt;name&gt;</code> element to which that specific property will be mapped; for example, <code>"name[1]/text"</code> will map the <code>firstName</code> property to the first <code>&lt;name&gt;</code> element.</p>
</li>
</ol>
<p>The object should look like <a href="#BEICHICJ">Example 15-19</a>.</p>
<div class="example"><a id="BEICHICJ" name="BEICHICJ"></a><a id="TLADG349" name="TLADG349"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 15-19 Customer Object Mapping Values by Position</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlType(propOrder={"firstName", "lastName"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlPath("name[1]/text()")
private String firstName;
@XmlPath("name[2]/text()")
private String lastName;
...
}
</pre></div>
<!-- class="example" -->
<p>For more information about using XPath predicates, see <a href="jpatoxml005.htm#BEIBFCFC">Using XPath Predicates for Mapping</a>.</p>
</div>
<!-- class="sect4" --></div>
<!-- class="sect3" --></div>
<!-- class="sect2" --></div>
<!-- class="sect1" --></div>
<!-- class="ind" -->
<!-- Start Footer -->
<div class="footer">
<hr />
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<col width="33%" />
<col width="*" />
<col width="33%" />
<tr>
<td valign="bottom">
<table class="navigation simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100" align="center">
<col width="*" />
<col width="48%" />
<col width="48%" />
<tr>
<td>&nbsp;</td>
<td align="center"><a href="jpatoxml002.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td align="center"><a href="jpatoxml004.htm"><img src="../../dcommon/images/rarrow.png" alt="Next" border="0" height="16" width="16" /></a></td>
</tr>
</table>
</td>
<td align="center" width="34%"><a href="http://www.eclipse.org/eclipselink/" title="EclipseLink home"><img src="../../dcommon/images/ellogo.png" alt="EclipseLink" width="150" border="0" /></a><br />
<font size="-2">Copyright&nbsp;&copy;&nbsp;2014,&nbsp;Oracle&nbsp;and/or&nbsp;its&nbsp;affiliates.&nbsp;All&nbsp;rights&nbsp;reserved.</font> <a href="../../dcommon/html/cpyr.htm"><br />
<td valign="bottom" align="right">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="225">
<tr>
<td>&nbsp;</td>
<td align="center" valign="top"><a href="toc.htm"><img src="../../dcommon/images/contents.png" alt="Go To Table Of Contents" border="0" height="16" width="16" /><br />
</td><td>&nbsp;</td><td align="center"><a href="../../" target="_top" class="external text" title="Search" rel="nofollow"><img src="../../dcommon/images/search.png" alt="Search" style="border:0;" /><br /><span class="mini"></span></a></td><td>&nbsp;</td><td align="center"><a href="../eclipselink_otlcg.pdf" title="PDF" target="_blank"><img src="../../dcommon/images/pdf_icon.png" style="padding-right:5px;border:0" alt="PDF"></a></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!-- class="footer" -->
<div id="copyright">Copyright &copy; 2014 by The Eclipse Foundation under the <a href="http://www.eclipse.org/org/documents/epl-v10.php">Eclipse Public License (EPL)</a><br /> <script type="text/javascript">var LastUpdated = document.lastModified;document.writeln ("Updated: " + LastUpdated);</script> </div><!-- START: Analytics --><script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1608008-2']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script><!-- END: Analytics --><!-- START: Sharethis --><script>var options={ "publisher": "e2fe9e07-fab6-4f84-83ea-0991b429842c", "position": "right", "ad": { "visible": false, "openDelay": 5, "closeDelay": 0}};var st_hover_widget = new sharethis.widgets.hoverbuttons(options);</script><!-- END: Sharethis --></body>
</html>