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.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Querying Objects by XPath"
< EclipseLink | UserGuide | MOXy | Runtime
m (→Querying Objects by XPath) |
|||
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. | 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. | ||
Line 10: | Line 12: | ||
<source lang="Java"> | <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> | </source> | ||
You could use XPath to access these values: | You could use XPath to access these values: | ||
+ | |||
<source lang="Java"> | <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> | </source> | ||
Revision as of 14:06, 25 April 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
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);