blob: ca0d1a744978b8189e789faf09c497bb6bb15418 [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 Values | EclipseLink 2.7 EclipseLink MOXy</title>
<meta name="generator" content="Oracle DARB XHTML Converter (Mode = document) - Version 1.0.22 Build 1" />
<meta name="date" content="2014-06-10T10:25:32Z" />
<meta name="robots" content="noarchive" />
<meta name="doctitle" content="Mapping Simple Values" />
<meta name="relnum" content="Release 2.7" />
<meta name="partnum" content="" />
<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="simple_values.htm" title="Previous" type="text/html" />
<link rel="next" href="simple_values002.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:1hjujor5_fg', 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"><div class="booktitle">Developing JAXB Applications Using EclipseLink MOXy,
<b>Release 2.7</b><br /></font></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_moxy.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="simple_values.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td align="center"><a href="simple_values002.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="CHDHEJHH" name="CHDHEJHH"></a><a id="TLJAX213" name="TLJAX213"></a>
<div class="sect1">
<h1 class="sect1"><font face="arial, helvetica, sans-serif" color="#330099">Mapping Simple Values</font></h1>
<p>This section demonstrates several ways to map simple Java values directly to XML text nodes.</p>
<ul>
<li>
<p><a href="#CHDGCAGJ">Mapping to an Attribute</a></p>
</li>
<li>
<p><a href="#CHDDCCGG">Mapping to a Text Node</a></p>
</li>
<li>
<p><a href="#CHDGEBBJ">Mapping to a Specified Schema Type</a></p>
</li>
<li>
<p><a href="#CHDDBJBE">Mapping with a Simple Type Translator</a></p>
</li>
</ul>
<a id="CHDGCAGJ" name="CHDGCAGJ"></a><a id="TLJAX214" name="TLJAX214"></a>
<div class="sect2">
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to an Attribute</font></h2>
<p>Given the XML schema in <a href="#CHDCEBGA">Example 4-1</a>, <a href="#CHDDEAAC">Figure 4-1</a> illustrates an XML direct mapping to an attribute in a corresponding XML document.</p>
<div class="example"><a id="CHDCEBGA" name="CHDCEBGA"></a><a id="TLJAX215" name="TLJAX215"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-1 Sample 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" -->
<div class="figure"><a id="CHDDEAAC" name="CHDDEAAC"></a><a id="TLJAX216" name="TLJAX216"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-1 XML Direct Mapping to an Attribute</font></em></strong></p>
<img src="img/dxmatt.gif" alt="XML Direct Mapping to an Attribute" title="XML Direct Mapping to an Attribute" longdesc="img_text/dxmatt.htm" /><br />
<a id="sthref65" name="sthref65" href="img_text/dxmatt.htm">Description of "Figure 4-1 XML Direct Mapping to an Attribute"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDCBCAE">Example 4-2</a> shows how to annotate your Java class to obtain this mapping with EclipseLink. All that is needed is the standard JAXB <code>@XmlAttribute</code> annotation.</p>
<div class="example"><a id="CHDCBCAE" name="CHDCBCAE"></a><a id="TLJAX217" name="TLJAX217"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-2 Using the @XmlAttribute Annotation</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 {
<strong>@XmlAttribute</strong>
private Integer id;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDEBIBI">Example 4-3</a> shows how to define your mapping information in EclipseLink's OXM metadata format.</p>
<div class="example"><a id="CHDEBIBI" name="CHDEBIBI"></a><a id="TLJAX218" name="TLJAX218"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-3 Defining the Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-attribute java-attribute="id"/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect2" -->
<a id="CHDDCCGG" name="CHDDCCGG"></a><a id="TLJAX219" name="TLJAX219"></a>
<div class="sect2">
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to a Text Node</font></h2>
<p>This section describes using an XML direct mapping when doing the following:</p>
<ul>
<li>
<p><a href="#CHDCEJEI">Mapping to a Text Node in a Simple Sequence</a></p>
</li>
<li>
<p><a href="#CHDHJFIB">Mapping to a Text Node in a Sub-element</a></p>
</li>
<li>
<p><a href="#CHDEEABA">Mapping to a Text Node by Position</a></p>
</li>
<li>
<p><a href="#CHDHFDGE">Mapping to a Simple Text Node</a></p>
</li>
</ul>
<a id="CHDCEJEI" name="CHDCEJEI"></a><a id="TLJAX220" name="TLJAX220"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to a Text Node in a Simple Sequence</font></h3>
<p>Given the XML schema in <a href="#CHDCJCDA">Example 4-4</a>, <a href="#CHDIHEFA">Figure 4-2</a> illustrates an XML direct mapping to individual text nodes in a sequence in a corresponding XML document.</p>
<div class="example"><a id="CHDCJCDA" name="CHDCJCDA"></a><a id="TLJAX221" name="TLJAX221"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-4 Sample 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:sequence&gt;
&lt;xsd:element name="first-name" type="xsd:string"/&gt;
&lt;xsd:element name="last-name" type="xsd:string"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDIHEFA" name="CHDIHEFA"></a><a id="TLJAX222" name="TLJAX222"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-2 XML Direct Mapping to Individual Text Nodes</font></em></strong></p>
<img src="img/dxmss.gif" alt="XML Direct Mapping to Individual Text Nodes" title="XML Direct Mapping to Individual Text Nodes" longdesc="img_text/dxmss.htm" /><br />
<a id="sthref66" name="sthref66" href="img_text/dxmss.htm">Description of "Figure 4-2 XML Direct Mapping to Individual Text Nodes"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDCEBGA">Example 4-1</a> shows how to annotate your Java class to obtain this mapping with EclipseLink. Here, the standard JAXB <code>@XmlElement</code> annotation is used, with a customized element name.</p>
<div class="example"><a id="CHDHHJGH" name="CHDHHJGH"></a><a id="TLJAX223" name="TLJAX223"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-5 Using the @XmlElement Annotation</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 {
<strong>@XmlElement</strong>(name="first-name")
private String firstName;
<strong>@XmlElement(</strong>name="last-name")
private String lastName;
...
}
</pre></div>
<!-- class="example" -->
<p>In <a href="#CHDHHJGH">Example 4-5</a>, we have explicitly specified the XML element names for the mapped attributes. This is an optional configuration &ndash; without an explicit name set, the XML element will simply match the Java attribute name; we would see <code>&lt;firstName&gt;Jane&lt;/firstName&gt;</code> in XML. For more information on JAXB name-binding algorithms, see "Appendix D: Binding XML Names to Java Identifiers" of the Java Architecture for XML Binding (JAXB) Specification (http://jcp.org/en/jsr/detail?id=222).</p>
<p><a href="#CHDIEJJD">Example 4-6</a> shows how to define your mapping information in EclipseLink's OXM metadata format. To specify a custom element <code>name</code>, the <code>name</code> attribute is used.</p>
<div class="example"><a id="CHDIEJJD" name="CHDIEJJD"></a><a id="TLJAX224" name="TLJAX224"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-6 Using the name Attribute</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="firstName" name="first-name"/&gt;
&lt;xml-element java-attribute="lastName" name="last-name"/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect3" -->
<a id="CHDHJFIB" name="CHDHJFIB"></a><a id="TLJAX225" name="TLJAX225"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to a Text Node in a Sub-element</font></h3>
<p>Given the XML schema in <a href="#CHDGBBJB">Example 4-7</a>, <a href="#CHDJGBDD">Figure 4-3</a> illustrates an XML direct mapping to a text node in a subelement in a corresponding XML document.</p>
<div class="example"><a id="CHDGBBJB" name="CHDGBBJB"></a><a id="TLJAX226" name="TLJAX226"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-7 Sample 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:sequence&gt;
&lt;xsd:element name="personal-info"&gt;
&lt;xsd:complexType&gt;
&lt;xsd:sequence&gt;
&lt;xsd:element name="first-name" type="xsd:string"/&gt;
&lt;xsd:element name="last-name" type="xsd:string"/&gt;
&lt;xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:element&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDJGBDD" name="CHDJGBDD"></a><a id="TLJAX227" name="TLJAX227"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-3 XML Direct Mapping to a Text Element in a Subnode</font></em></strong></p>
<img src="img/dxmse.gif" alt="XML Direct Mapping to a Text Element in a Subnode" title="XML Direct Mapping to a Text Element in a Subnode" longdesc="img_text/dxmse.htm" /><br />
<a id="sthref67" name="sthref67" href="img_text/dxmse.htm">Description of "Figure 4-3 XML Direct Mapping to a Text Element in a Subnode"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDJGGDB">Example 4-8</a> shows how to annotate your Java class to obtain this mapping with EclipseLink. Here, because we are going beyond a simple element name customization and are actually introducing new XML structure, EclipseLink's <code>@XmlPath</code> annotation is used.</p>
<div class="example"><a id="CHDJGGDB" name="CHDJGGDB"></a><a id="TLJAX228" name="TLJAX228"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-8 Using the @XmlPath Annotation</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 {
<strong>@XmlPath</strong>("personal-info/first-name/text()")
private String firstName;
<strong>@XmlPath</strong>("personal-info/last-name/text()")
private String lastName;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDCJJCH">Example 4-9</a> shows how to define your mapping information in EclipseLink's OXM metadata format. Here, the customized XML path is defined in the <code>xml-path</code> attribute.</p>
<div class="example"><a id="CHDCJJCH" name="CHDCJJCH"></a><a id="TLJAX229" name="TLJAX229"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-9 Using the xml-path Attribute</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="firstName" <strong>xml-path</strong>="personal-info/first-name/text()"/&gt;
&lt;xml-element java-attribute="lastName" <strong>xml-path</strong>="personal-info/last-name/text()"/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect3" -->
<a id="CHDEEABA" name="CHDEEABA"></a><a id="TLJAX230" name="TLJAX230"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to a Text Node by Position</font></h3>
<p>Given the XML schema in <a href="#CHDEDHDH">Example 4-10</a>, <a href="#CHDCDJAA">Figure 4-4</a> illustrates an XML direct mapping to a text node by position in a corresponding XML document.</p>
<div class="example"><a id="CHDEDHDH" name="CHDEDHDH"></a><a id="TLJAX231" name="TLJAX231"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-10 Sample 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:sequence&gt;
&lt;xsd:element name="name" type="xsd:string" maxOccurs="2"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDCDJAA" name="CHDCDJAA"></a><a id="TLJAX232" name="TLJAX232"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-4 XML Direct Mapping to a Text Node</font></em></strong></p>
<img src="img/dxmpos.gif" alt="XML Direct Mapping to a Text Node" title="XML Direct Mapping to a Text Node" longdesc="img_text/dxmpos.htm" /><br />
<a id="sthref68" name="sthref68" href="img_text/dxmpos.htm">Description of "Figure 4-4 XML Direct Mapping to a Text Node"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDGIGCD">Example 4-11</a> shows how to configure this mapping in Java. Again, for more complex XML path customization, EclipseLink's <code>@XmlPath</code> annotation is used.</p>
<div class="example"><a id="CHDGIGCD" name="CHDGIGCD"></a><a id="TLJAX233" name="TLJAX233"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-11 Using the @XmlPath Annotation</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 {
<strong>@XmlPath</strong>("name[1]/text()")
private String firstName;
<strong>@XmlPath</strong>("name[2]/text()")
private String lastName;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDGDFJB">Example 4-12</a> shows how to define your mapping information in EclipseLink's OXM metadata format.</p>
<div class="example"><a id="CHDGDFJB" name="CHDGDFJB"></a><a id="TLJAX234" name="TLJAX234"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-12 Defining the Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="firstName" xml-path="name[1]/text()"/&gt;
&lt;xml-element java-attribute="lastName" xml-path="name[2]/text()"/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect3" -->
<a id="CHDHFDGE" name="CHDHFDGE"></a><a id="TLJAX235" name="TLJAX235"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to a Simple Text Node</font></h3>
<p>Given the XML schema in <a href="#CHDJIBEE">Example 4-13</a>, <a href="#CHDBJFCA">Figure 4-5</a> illustrates an XML direct mapping to a simple text node in a corresponding XML document.</p>
<div class="example"><a id="CHDJIBEE" name="CHDJIBEE"></a><a id="TLJAX236" name="TLJAX236"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-13 Sample 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="phone-number" type="xsd:string"/&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDBJFCA" name="CHDBJFCA"></a><a id="TLJAX237" name="TLJAX237"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-5 XML Direct Mapping to a Simple Text Node</font></em></strong></p>
<img src="img/dxmstn.gif" alt="XML Direct Mapping to a Simple Text Node" title="XML Direct Mapping to a Simple Text Node" longdesc="img_text/dxmstn.htm" /><br />
<a id="sthref69" name="sthref69" href="img_text/dxmstn.htm">Description of "Figure 4-5 XML Direct Mapping to a Simple Text Node"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDGGIAD">Example 4-14</a> shows how to annotate your Java class to obtain this mapping with EclipseLink. In this case, the <code>@XmlValue</code> annotation will be used.</p>
<div class="example"><a id="CHDGGIAD" name="CHDGGIAD"></a><a id="TLJAX238" name="TLJAX238"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-14 Using the @XmlValue Annotation</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 {
<strong>@XmlValue</strong>
private String number;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDDJDIJ">Example 4-15</a> shows how to define your mapping information in EclipseLink's OXM metadata format.</p>
<div class="example"><a id="CHDDJDIJ" name="CHDDJDIJ"></a><a id="TLJAX239" name="TLJAX239"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-15 Defining the Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="PhoneNumber"&gt;
&lt;xml-root-element name="phone-number"/&gt;
&lt;java-attributes&gt;
&lt;xml-value java-attribute="number"/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect3" --></div>
<!-- class="sect2" -->
<a id="CHDGEBBJ" name="CHDGEBBJ"></a><a id="TLJAX240" name="TLJAX240"></a>
<div class="sect2">
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping to a Specified Schema Type</font></h2>
<p>In most cases, EclipseLink can determine the target format in the XML document. However, there are cases where you must specify which one of a number of possible targets EclipseLink should use. For example, a <code>java.util.Calendar</code> could be marshalled to a schema <code>date</code>, <code>time</code>, or <code>dateTime</code> node, or a <code>byte[]</code> could be marshalled to a schema <code>hexBinary</code> or <code>base64Binary</code> node.</p>
<p>The XML schema in <a href="#CHDGDFEB">Example 4-16</a> and <a href="#CHDIFDIB">Figure 4-6</a> an XML direct mapping from a <strong>Calendar</strong> object in Java to a <strong>date</strong> field in XML.</p>
<div class="example"><a id="CHDGDFEB" name="CHDGDFEB"></a><a id="TLJAX241" name="TLJAX241"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-16 Sample 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:sequence&gt;
&lt;xsd:element name="hire-date" type="xsd:date"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDIFDIB" name="CHDIFDIB"></a><a id="TLJAX242" name="TLJAX242"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-6 XML Direct Mapping to a Specific Schema Type</font></em></strong></p>
<img src="img/schematypedate.gif" alt="XML Direct Mapping to a Specific Schema Type" title="XML Direct Mapping to a Specific Schema Type" longdesc="img_text/schematypedate.htm" /><br />
<a id="sthref70" name="sthref70" href="img_text/schematypedate.htm">Description of "Figure 4-6 XML Direct Mapping to a Specific Schema Type"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDDECHC">Example 4-17</a> shows how to annotate your Java class to obtain this mapping with EclipseLink. Here, the <code>@XmlSchemaType</code> is used to specify the datatype that will appear in the marshalled XML.</p>
<div class="example"><a id="CHDDECHC" name="CHDDECHC"></a><a id="TLJAX243" name="TLJAX243"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-17 Using the @XmlSchemaType Annotation</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="hire-date")
<strong>@XmlSchemaType</strong>(name="date")
private Calendar hireDate;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDBCDGI">Example 4-18</a> shows how to define your mapping information in EclipseLink's OXM metadata format.</p>
<div class="example"><a id="CHDBCDGI" name="CHDBCDGI"></a><a id="TLJAX244" name="TLJAX244"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-18 Defining the Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="hireDate" name="hire-date"&gt;
&lt;xml-schema-type name="date"/&gt;
&lt;/xml-element&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" -->
<a id="TLJAX245" name="TLJAX245"></a>
<div class="sect3"><a id="sthref71" name="sthref71"></a>
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Using Java Type Adapters</font></h3>
<p><a href="#CHDGFHHD">Example 4-19</a> and <a href="#CHDJHJDG">Figure 4-7</a> illustrate XML direct mappings to two different text nodes of different binary types.</p>
<div class="example"><a id="CHDGFHHD" name="CHDGFHHD"></a><a id="TLJAX246" name="TLJAX246"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-19 Sample 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:sequence&gt;
&lt;xsd:element name="resume" type="xsd:base64Binary"/&gt;
&lt;xsd:element name="picture" type="xsd:hexBinary"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDJHJDG" name="CHDJHJDG"></a><a id="TLJAX247" name="TLJAX247"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-7 XML Direct Mappings to Different Text Nodes of Different Binary Types</font></em></strong></p>
<img src="img/dxmscht.gif" alt="XML Direct Mappings" title="XML Direct Mappings" longdesc="img_text/dxmscht.htm" /><br />
<a id="sthref72" name="sthref72" href="img_text/dxmscht.htm">Description of "Figure 4-7 XML Direct Mappings to Different Text Nodes of Different Binary Types"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDJGHBH">Example 4-20</a> shows how to annotate your Java class to obtain this mapping with EclipseLink. By default, JAXB will marshall <code>byte[]</code> to <code>base64Binary</code>, so nothing special is needed for the resume mapping. To map to a <strong>hexBinary</strong> field, the <code>@XmlSchemaType</code> annotation specifies the XML type, while <code>@XmlJavaTypeAdapter</code> specifies the adapter class that will be responsible for converting the value (in this case, the built-in JAXB <code>HexBinaryAdapter</code>).</p>
<div class="example"><a id="CHDJGHBH" name="CHDJGHBH"></a><a id="TLJAX248" name="TLJAX248"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-20 Using the @XmlSchemaType and @XmlJavaTypeAdapter Annotations</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 {
private byte[] resume;
<strong>@XmlSchemaType</strong>(name="hexBinary")
<strong>@XmlJavaTypeAdapter</strong>(HexBinaryAdapter.class)
private byte[] picture;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDCIEED">Example 4-21</a> shows how to define your mapping information in EclipseLink's OXM metadata format.</p>
<div class="example"><a id="CHDCIEED" name="CHDCIEED"></a><a id="TLJAX249" name="TLJAX249"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-21 Defining the Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="Customer"&gt;
&lt;xml-root-element name="customer"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="resume"/&gt;
&lt;xml-element java-attribute="picture"&gt;
&lt;xml-schema-type name="hexBinary"/&gt;
&lt;xml-java-type-adapter value="javax.xml.bind.annotation.adapters.HexBinaryAdapter"/&gt;
&lt;/xml-element&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></div>
<!-- class="sect3" --></div>
<!-- class="sect2" -->
<a id="CHDDBJBE" name="CHDDBJBE"></a><a id="TLJAX250" name="TLJAX250"></a>
<div class="sect2">
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping with a Simple Type Translator</font></h2>
<p>If the type of a node is not defined in your XML schema, you can configure an XML direct mapping to use the <code>xsi:type</code> attribute to provide type information.</p>
<p>Given the XML schema in <a href="#CHDGEJDD">Example 4-22</a>, <a href="#CHDGACBA">Figure 4-8</a> illustrates a Java class that can be mapped to a corresponding XML document.</p>
<div class="example"><a id="CHDGEJDD" name="CHDGEJDD"></a><a id="TLJAX251" name="TLJAX251"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-22 Sample 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="phone-number" type="phone-number-type"/&gt;
&lt;xsd:complexType name="phone-number-type"&gt;
&lt;xsd:sequence&gt;
&lt;xsd:element name="area-code" type="anySimpleType"/&gt;
&lt;xsd:element name="number" type="anySimpleType"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
</pre></div>
<!-- class="example" -->
<div class="figure"><a id="CHDGACBA" name="CHDGACBA"></a><a id="TLJAX252" name="TLJAX252"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-8 Sample Java Class</font></em></strong></p>
<img src="img/dxmsttc.gif" alt="Sample Java Class" title="Sample Java Class" longdesc="img_text/dxmsttc.htm" /><br />
<a id="sthref73" name="sthref73" href="img_text/dxmsttc.htm">Description of "Figure 4-8 Sample Java Class"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDFJIEB">Figure 4-9</a> illustrates an XML direct mapping with a simple type translator in an XML document that conforms to the schema above.</p>
<div class="figure"><a id="CHDFJIEB" name="CHDFJIEB"></a><a id="TLJAX253" name="TLJAX253"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Figure 4-9 XML Direct Mapping with Simple Type Translator</font></em></strong></p>
<img src="img/dxmsttm.gif" alt="XML Direct Mapping with Simple Type Translator" title="XML Direct Mapping with Simple Type Translator" longdesc="img_text/dxmsttm.htm" /><br />
<a id="sthref74" name="sthref74" href="img_text/dxmsttm.htm">Description of "Figure 4-9 XML Direct Mapping with Simple Type Translator"</a><br />
<br /></div>
<!-- class="figure" -->
<p><a href="#CHDGCAAG">Example 4-23</a> shows how to annotate your Java class to obtain this mapping with EclipseLink.</p>
<div class="example"><a id="CHDGCAAG" name="CHDGCAAG"></a><a id="TLJAX254" name="TLJAX254"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-23 Sample Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package example;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="phone-number")
public class PhoneNumber {
@XmlElement(name="area-code")
private Object areaCode;
private Object number;
...
}
</pre></div>
<!-- class="example" -->
<p><a href="#CHDHEGAE">Example 4-24</a> shows how to define your mapping information in EclipseLink's OXM metadata format.</p>
<div class="example"><a id="CHDHEGAE" name="CHDHEGAE"></a><a id="TLJAX255" name="TLJAX255"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 4-24 Sample XML Mapping</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
...
&lt;java-type name="PhoneNumber"&gt;
&lt;xml-root-element name="phone-number"/&gt;
&lt;java-attributes&gt;
&lt;xml-element java-attribute="areaCode" name="area-code"/&gt;
&lt;xml-element java-attribute="number"/&gt;
&lt;/java-attributes&gt;
&lt;/java-type&gt;
...
</pre></div>
<!-- class="example" --></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="simple_values.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td align="center"><a href="simple_values002.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 />
<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_moxy.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; 2013 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>