09 February, 2010

Java and XPath


XPath is an alternate way of accessing elements and attributes of XML document. XPath is a standard recommended by W3C. XPath looks like unix path separated by '/'.

XPath has got many built-in functions for navigating Nodes and Attributes of XML. XQuery and XPointer was built on top of XPath.



Example XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="english">Harry Potter 2 </title>
  <price>40</price>
</book>
<book>
  <title lang="german">Learning XML</title>
  <price>50</price>
</book>
</bookstore>

XPath Expression: /bookstore/book/title[@lang='german']

This XPath expression retrieves all the book titles with language German. The XPath helps in retrieving the required data with a single expression.

Now let's see how we can use this in Java to parse XML documents.

JDK comes with a separate package java.xml.xpath from version 1.5 onwards. So, no need to have any external libraries to be included into the classpath.

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("catalogue.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/bookstore/book/title[@lang='german']");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++)
{
        System.out.println(nodes.item(i).getNodeValue());
      }}
catch(Exception ex){
    ex.printStackTrace();}

The above code snippet give the result of all the book titles which has lang='german'. The power of XPath lies in constructing the expression to retrieve right information.

0 comments: