Skip to main content
Jump to: navigation, search


< EclipseLink‎ | Examples‎ | MOXy
Revision as of 11:22, 15 June 2011 by (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


As of EclipseLink MOXy 2.3, elements can be mapped based on the value of an attribute. This is done by leveraging XPath predicates via MOXy's @XmlPath annotation.

Usually node names (and namespace URI) uniquely map to fields and properties in domain classes:

<?xml version="1.0" encoding="UTF-8"?>
        <street>123 A Street</street>
    <phone-number type="work">555-1111</phone-number>
    <phone-number type="cell">555-2222</phone-number>

However, there are use cases where the qualifier is something like an XML attribute. Note in the XML below how all of the XML elements are called "node", and the qualifier is an attribute called "name":

<?xml version="1.0" encoding="UTF-8"?>
   <node name="first-name">Jane</node>
   <node name="last-name">Doe</node>
   <node name="address">
      <node name="street">123 A Street</node>
   <node name="phone-number" type="work">555-1111</node>
   <node name="phone-number" type="cell">555-2222</node>

Java Model with @XmlPath annotation

The following domain model will be used for this example. The get and set methods have been omitted. Notice how the @XmlPath annotation is used to extend the JAXB metadata.


package examples;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlType(propOrder={"firstName", "lastName", "address", "phoneNumbers"})
public class Customer {
    private String firstName;
    private String lastName;
    private Address address;
    private List<PhoneNumber> phoneNumbers;


package examples;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
public class Address {
    private String street;


package examples;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class PhoneNumber {
    private String type;
    private String number;

External Bindings File

Instead of specifying the @XmlPath annotations on the domain model, the information could alternatively be specified in an external bindings file:

<xml-bindings xmlns="">
      <java-type name="examples.Customer">
            <xml-element java-attribute="firstName" xml-path="node[@name='first-name']/text()"/>
            <xml-element java-attribute="lastName" xml-path="node[@name='last-name']/text()"/>
            <xml-element java-attribute="address" name="node[@name='address']"/>
            <xml-element java-attribute="phoneNumbers" name="node[@name='phone-number']"/>
      <java-type name="examples.Address">
            <xml-element java-attribute="street" xml-path="node[@name='street']/text()"/>

For more information on how to use and specify a bindings file, see the EclipseLink-OXM.XML example

Back to the top