Jump to: navigation, search

EclipseLink/Examples/MOXy/ElementMappingByAttributeValue

Introduction

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"?>
<customer>
    <first-name>Jane</first-name>
    <last-name>Doe</last-name>
    <address>
        <street>123 A Street</street>
    </address>
    <phone-number type="work">555-1111</phone-number>
    <phone-number type="cell">555-2222</phone-number>
</customer>

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>
   <node name="first-name">Jane</node>
   <node name="last-name">Doe</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>

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.

Customer

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;
 
@XmlRootElement(name="node")
@XmlType(propOrder={"firstName", "lastName", "address", "phoneNumbers"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlPath("node[@name='first-name']/text()")
    private String firstName;
 
    @XmlPath("node[@name='last-name']/text()")
    private String lastName;
 
    @XmlPath("node[@name='address']")
    private Address address;
 
    @XmlPath("node[@name='phone-number']")
    private List<PhoneNumber> phoneNumbers;
 
}

Address

package examples;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
 
import org.eclipse.persistence.oxm.annotations.XmlPath;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
 
    @XmlPath("node[@name='street']/text()")
    private String street;
 
}

PhoneNumber

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;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
 
    @XmlAttribute
    private String type;
 
    @XmlValue
    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="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
   <java-types>
      <java-type name="examples.Customer">
         <java-attributes>
            <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-attributes>
      </java-type>
      <java-type name="examples.Address">
         <java-attributes>
            <xml-element java-attribute="street" xml-path="node[@name='street']/text()"/>
         </java-attributes>
      </java-type>
   </java-types>
</xml-bindings>

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