Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Querying Objects by XPath"

From Eclipsepedia

Jump to: navigation, search
m (re-added footer)
Line 14: Line 14:
 
For example, consider the following XML document:
 
For example, consider the following XML document:
  
 +
<div style="width:700px">
 
<source lang="xml">
 
<source lang="xml">
 
<customer id="1141">
 
<customer id="1141">
Line 24: Line 25:
 
</customer>
 
</customer>
 
</source>
 
</source>
 +
</div>
  
  
 
Your typical application code might look something like this:
 
Your typical application code might look something like this:
  
 +
<div style="width:700px">
 
<source lang="java">
 
<source lang="java">
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
Line 37: Line 40:
 
jaxbContext.createMarshaller().marshal(customer, System.out);
 
jaxbContext.createMarshaller().marshal(customer, System.out);
 
</source>
 
</source>
 +
</div>
  
  
 
You could instead use XPath to access these values:
 
You could instead use XPath to access these values:
  
 +
<div style="width:700px">
 
<source lang="java">
 
<source lang="java">
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
Line 50: Line 55:
 
jaxbContext.createMarshaller().marshal(customer, System.out);
 
jaxbContext.createMarshaller().marshal(customer, System.out);
 
</source>
 
</source>
 +
</div>
  
  

Revision as of 13:11, 11 July 2011

EclipseLink MOXy

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source


Querying Objects by XPath

In addition to using conventional Java access methods to get and set your object's values, EclipseLink MOXy also allows you to access values using an XPath statement. There are special APIs on EclipseLink's JAXBContext to allow you to get and set values by XPath.


For example, consider the following XML document:

<customer id="1141">
   <first-name>Jon</first-name>
   <last-name>Smith</last-name>
   <phone-number>
      <area-code>515</area-code>
      <number>2726652</number>
   </phone-number>
</customer>


Your typical application code might look something like this:

Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
...
int customerId = customer.getId();
customer.setFirstName("Bob");
customer.getPhoneNumber().setAreaCode("555");
...
jaxbContext.createMarshaller().marshal(customer, System.out);


You could instead use XPath to access these values:

Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
...
int customerId = jaxbContext.getValueByXPath(customer, "@id", null, Integer.class);
jaxbContext.setValueByXPath(customer, "first-name/text()", null, "Bob");
jaxbContext.setValueByXPath(customer, "phone-number/area-code/text()", null, "555");
...
jaxbContext.createMarshaller().marshal(customer, System.out);


Eclipselink-logo.gif
Version: DRAFT
Other versions...