XPath Introduction
XPath is a language for defining parts of an XML document. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document.
The primary syntactic construct in XPath is the expression. An XPath expression is evaluated to yield an object which has one of the following four basic types.
- Node sets - Collection of nodes
- Boolean
- Number or
- String
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
XPath Examples
Example
|
XPath Expression
|
Output
|
Get title of book with id as “bk02” | /catalog/book[@id='bk102']/title | Midnight Rain |
Get title of all books | /catalog/book/title | XML Developer's Guide Midnight Rain Maeve Ascendant |
Get title of all books with genre as “Fantasy” | /catalog/book[genre = 'Fantasy']/title | Midnight Rain Maeve Ascendant |
Java program to parse XML file using XPath expressions
To understand this program we need basics of DOM parser implementation. Refer DOM Parser to read XML file. Java supports XPath package which is pretty straightforward to use once we understand XPath expressions.Implementation to evaluate XPath expressions
package com.sourcetricks.xpath; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XPathTest { public static void main(String[] args) { try { // Setup the parser DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Read the XML file File inputFile = new File("resources/input.xml"); InputStream inputStream = new FileInputStream(inputFile); // Parse the XML file Document doc = builder.parse(inputStream); // Create an XPath instance XPath xPath = XPathFactory.newInstance().newXPath(); // Evaluate simple expressions String expression1 = "/catalog/book[@id='bk102']/title"; String author = xPath.evaluate(expression1, doc); System.out.println("<<< Using XPath to read node values >>>"); System.out.println(author); // Evaluate XPath expression and get multiple values String expression2 = "/catalog/book/title"; System.out.println("<<< Using XPath to read set of node values >>>"); NodeList titleList = (NodeList) xPath.compile(expression2).evaluate(doc, XPathConstants.NODESET); for ( int i = 0; i < titleList.getLength(); i++ ) { System.out.println(titleList.item(i).getTextContent()); } // Evaluate XPath expression based on another nodes value String expression3 = "/catalog/book[genre = 'Fantasy']/title"; System.out.println("<<< Using XPath to read set of node values with specific values >>>"); titleList = (NodeList) xPath.compile(expression3).evaluate(doc, XPathConstants.NODESET); for ( int i = 0; i < titleList.getLength(); i++ ) { System.out.println(titleList.item(i).getTextContent()); } } catch ( FileNotFoundException e ) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } }Output:-
<<< Using XPath to read node values >>> Midnight Rain <<< Using XPath to read set of node values >>> XML Developer's Guide Midnight Rain Maeve Ascendant <<< Using XPath to read set of node values with specific values >>> Midnight Rain Maeve Ascendant
0 comments:
Post a Comment