Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/ElementMappingByAttributeValue"

m
m
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
As of EclipseLink 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.
+
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/properties in or domain classes:
+
Usually node names (and namespace URI) uniquely map to fields and properties in domain classes:
  
 
<source lang="xml">
 
<source lang="xml">
Line 17: Line 17:
 
</source>
 
</source>
  
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".
+
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":
  
 
<source lang="xml">
 
<source lang="xml">
Line 33: Line 33:
  
 
== Java Model with @XmlPath annotation ==
 
== Java Model with @XmlPath annotation ==
The following domain model will be used for this example.  The get/set methods have been omitted. Notice how the @XmlPath annotation is used to extend the JAXB metadata.
+
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'''
 
'''Customer'''
 +
 
<source lang="java">
 
<source lang="java">
 
package examples;
 
package examples;
Line 109: Line 110:
 
</source>
 
</source>
  
== External bindings file ==
+
== External Bindings File ==
Instead of specifying the @XMLPath annotations on the domain model the information could alternatively be specified in an external bindings file.
+
Instead of specifying the '''@XmlPath''' annotations on the domain model, the information could alternatively be specified in an external bindings file:
  
 
<source lang="xml">
 
<source lang="xml">
Line 132: Line 133:
 
</source>
 
</source>
  
For more information on how to use and specify a bindings file see the [http://wiki.eclipse.org/index.php?title=EclipseLink/Examples/MOXy/EclipseLink-OXM.XML EclipseLink-OXM.XML example]
+
For more information on how to use and specify a bindings file, see the [http://wiki.eclipse.org/index.php?title=EclipseLink/Examples/MOXy/EclipseLink-OXM.XML EclipseLink-OXM.XML example]

Latest revision as of 11:22, 15 June 2011

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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.