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/UserGuide/MOXy/Runtime/Querying Objects by XPath"

Line 8: Line 8:
  
 
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.
 
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:
 
For example, consider the following XML document:
Line 21: Line 22:
 
</customer>
 
</customer>
 
</source>
 
</source>
 +
  
 
Your typical application code might look something like this:
 
Your typical application code might look something like this:
Line 33: Line 35:
 
jaxbContext.createMarshaller().marshal(customer, System.out);
 
jaxbContext.createMarshaller().marshal(customer, System.out);
 
</source>
 
</source>
 +
  
 
You could instead use XPath to access these values:
 
You could instead use XPath to access these values:

Revision as of 14:28, 25 April 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug 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: 2.2.0 - DRAFT
Other versions...

Copyright © Eclipse Foundation, Inc. All Rights Reserved.