Jump to: navigation, search

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

m (New page: {{EclipseLink_UserGuide |info=y |eclipselink=y |eclipselinktype=MOXy }} ==Querying Objects by XPath== {{EclipseLink_MOXy |next=[[EclipseLink/UserGuide/MOXy/Runtime/Converting_XML_to_Obj...)
 
m
Line 4: Line 4:
 
|eclipselinktype=MOXy
 
|eclipselinktype=MOXy
 
}}
 
}}
==Querying Objects by XPath==
+
== 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.
  
 +
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>
  
  

Revision as of 09:26, 5 April 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

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.

For example, instead of:

    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 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...