Jump to: navigation, search

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

m
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] For the current release, see [http://www.eclipse.org/eclipselink/documentation/2.4/moxy Developing JAXB Applications Using EclipseLink MOXy, EclipseLink 2.4]
|info=y
+
'''
|eclipselink=y
+
|eclipselinktype=MOXy
+
}}
+
  
== 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.
+
http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime008.htm
 
+
 
+
For example, consider the following XML document:
+
 
+
<source lang="xml">
+
<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>
+
</source>
+
 
+
 
+
Your typical application code might look something like this:
+
 
+
<source lang="java">
+
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
+
...
+
int customerId = customer.getId();
+
customer.setFirstName("Bob");
+
customer.getPhoneNumber().setAreaCode("555");
+
...
+
jaxbContext.createMarshaller().marshal(customer, System.out);
+
</source>
+
 
+
 
+
You could instead use XPath to access these values:
+
 
+
<source lang="java">
+
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);
+
</source>
+
 
+
 
+
{{EclipseLink_MOXy
+
|next=[[EclipseLink/UserGuide/MOXy/Runtime/Converting_XML_to_Objects|Converting XML to Objects]]
+
|previous= [[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project/From_sessions.xml_using_DynamicEntities|From sessions.xml using DynamicEntities]]
+
|up= [[EclipseLink/UserGuide/MOXy/Runtime|Runtime]]
+
|version=2.2.0 - DRAFT}}
+

Latest revision as of 16:30, 6 November 2012

Warning For the current release, see Developing JAXB Applications Using EclipseLink MOXy, EclipseLink 2.4


http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime008.htm