In the previous article we have explained briefly the XML Schema elements, marshalling, unmarshalling and validating XML documents. This article will explain how we can use XPath together with JAXB. JAXB does not support XPath directly but one can use other XPath providers to overcome this deficiency.
XPath
XPath provides an efficient way to traverse the object graph which represents the XML document and avoid large Java code. A good tutorial of XPath can be found here. There are several online tools available for verifying XPath expression such as this one.
As explained already the XPath expression can only be used with JAXB by other third parties provided interpreters. The two commonly used providers available for XPath are following:
Apache Commons JXPath
JXPath is an interpreter of XPath expression language and applies to all kind of objects such as JavaBeans, DOM, Maps etc.
Integrating and using JXPath is very easy. Just add the jar file in the class path and initialize JXPathContext in your java code to use XPath expression. To get single value of an XML element or attribute, JXPathContext provides a method i.e. getValue.
ProductExtract productExtract = getDataModel(); String xpath = "product/customer[@customerid='cust-1']/customerAddress"; String value = JXPathContext.newContext(productExtract).getValue(xpath);
For retrieving set of objects JXPathContext provides another method i.e. iterate.
ProductExtract productExtract = getDataModel(); String xpath = "//product[@type='" + ProductListingType.ELECTRONICS + "']"; List products = new ArrayList(); Iterator it = JXPathContext.newContext(productExtract).iterate(xpath); while(it.hasNext()) { Object obj = it.next(); if(obj instanceof ProductType) { products.add((ProductType)obj); } else { throw new Exception("Invalide object type"); } }
EclipseLink Moxy
The EclipseLink Moxy implements JAXB to bind Java Classes to XML Schema. One can use annotations to define the mappings, even for the complex structured XML documents.
Integrating Moxy is easy, there are several options which are given on download page. If you are using Maven to build Java project, integrating Moxy via maven artifact (link: http://wiki.eclipse.org/EclipseLink/Maven) is even easier. Just search the artifact with id “org.eclipse.persistence.moxy” and add it to POM.
To use EclipseLink Moxy in Java code, you need to identify Moxy as a JAXB implementation in current use by adding jaxb.properties file to the package containing model classes. This properties file defines the factory to be used to build new JAXBContexts and contains this property.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
When you start your program in debug mode you can find that JAXBContext is of type:
“org.eclipse.persistence.jaxb.JAXBContext”. Using XPath expressions with Moxy is exactly the same as it is with JXPath.
Conclusion
This article explains the usage of XPath expressions with JAXB, which provides effective means to retrieve data from XML document. Apache Commons JXPath can be used with default JAXB implementation while Eclipse Moxy uses its own implementation of JAXB.