Skip to main content

Notice: This Wiki is now read only and edits are no longer 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 9: Line 9:
 
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, instead of:
+
For example, consider the following XML document:
  
<source lang="Java">
+
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<xsd:schema
 +
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 +
 +
    <xsd:element name="customer">
 +
        <xsd:complexType>
 +
            <xsd:sequence>
 +
                <xsd:element
 +
                    name="date-of-birth"
 +
                    type="xsd:date"
 +
                    minOccurs="0"/>
 +
            </xsd:sequence>
 +
        </xsd:complexType>
 +
    </xsd:element>
 +
 +
</xsd:schema>
 +
</source>
 +
 
 +
<source lang="java">
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
  
Line 23: Line 42:
 
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);
 
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
  

Revision as of 14:19, 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:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
    <xsd:element name="customer">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element
                    name="date-of-birth"
                    type="xsd:date"
                    minOccurs="0"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
 
</xsd:schema>
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...

Back to the top