In the following example, our XML contains two '''name''' elements; the first occurrence of '''name''' should represent the '''Customer's''' first name, and the second '''name''' will be their last name. To map this, we will specify XPath expressions for each property that will match the appropriate XML element. Note that we also use '''@XmlType(propOrder)''' to ensure that our elements will always be in the proper positions.

+

−

+

−

<div style="width:700px">

+

−

<source lang="java">

+

−

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;

+

−

+

−

...

+

−

}

+

−

</source>

+

−

</div>

+

−

+

−

This same configuration can be expressed in an EclipseLink XML Bindings document as follows:

+

−

+

−

<div style="width:700px">

+

−

<source lang="xml">

+

−

...

+

−

<java-type name="Customer">

+

−

<xml-root-element/>

+

−

<xml-type prop-order="firstName lastName"/>

+

−

<java-attributes>

+

−

<xml-element java-attribute="firstName" xml-path="name[1]/text()"/>

+

−

<xml-element java-attribute="lastName" xml-path="name[2]/text()"/>

+

−

</java-attributes>

+

−

</java-type>

+

−

...

+

−

</source>

+

−

</div>

+

−

+

−

This will give us the desired XML representation:

+

−

+

−

<div style="width:700px">

+

−

<source lang="xml">

+

−

<?xml version="1.0" encoding="UTF-8"?>

+

−

<customer>

+

−

<name>Bob</name>

+

−

<name>Smith</name>

+

−

</customer>

+

−

</source>

+

−

</div>

+

−

+

−

+

−

== Mapping based on an Attribute Value ==

+

−

+

−

Since EclipseLink MOXy '''2.3''', you can also map to an XML element based on an Attribute value. In this example, all of our XML elements are named '''node''', differentiated by the value of their '''name''' attribute:

+

−

+

−

<div style="width:700px">

+

−

<source lang="xml">

+

−

<?xml version="1.0" encoding="UTF-8"?>

+

−

<node>

+

−

<node name="first-name">Bob</node>

+

−

<node name="last-name">Smith</node>

+

−

<node name="address">

+

−

<node name="street">123 A Street</node>

+

−

</node>

+

−

<node name="phone-number" type="work">555-1111</node>

+

−

<node name="phone-number" type="cell">555-2222</node>

+

−

</node>

+

−

</source>

+

−

</div>

+

−

+

−

We can use an XPath in the form of '''''element-name[@attribute-name='value']''''' to map each Java field:

EclipseLink allows you to configure your one-to-one mappings so the data from the target object will appear inside the source object's XML element. Expanding on the previous example, we could map the '''Address''' information so that it would appear directly under the '''customer''' element, and '''''not''''' wrapped in its own element. This is referred to as a "self" mapping, and is achieved by setting the target object's XPath to <tt>'''"."'''</tt> (dot).

+

−

+

−

The following example demonstrates a self mapping declared in annotations.