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

From Eclipsepedia

Jump to: navigation, search
m
 
(13 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 ==
 
  
With EclipseLink MOXy, you do not have to use normal Javabean access methods to '''get''' and '''set''' values. Instead, you can use the APIs on the JAXBContext itself, at runtime.
+
http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime008.htm
 
+
For example, instead of:
+
 
+
<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 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