XPath

XPath descendant example

In this example we shall talk about the Descendant axis specifier in XPath. An axis specifier indicates the direction of navigation in DOM tree of that XML document. This helps in effectively querying the XML document.

The Descendant axis selects all the descendants of the current node. The ‘descendant’ refers to the children and grand-children of the current node.

We will look at a few examples to see how descendant works.

Consider the XML file below for our example. The File has a number of <cricketer> nodes under the <cricketers> tag representing a cricket team:

cricketTeam_info.xml


<?xml version="1.0" encoding="UTF-8"?>

<cricketers>
	<cricketer type="righty">
		<name>MS Dhoni</name>
		<role>Captain</role>
		<position>Wicket-Keeper</position>
	</cricketer>
	<cricketer type="lefty">
		<name>Shikhar Dhawan</name>
		<role>Batsman</role>
		<position>Point</position>
	</cricketer>
	<cricketer type="righty">
		<name>Virat Kohli</name>
		<role>Batsman</role>
		<position>cover</position>
	</cricketer>
	<cricketer type="righty">
		<name>Shami</name>
		<role>Bowler</role>
		<position>SquareLeg</position>
	</cricketer>
	<cricketer type="lefty">
		<name>Zaheer Khan</name>
		<role>Bowler</role>
		<position>FineLeg</position>
	</cricketer>
</cricketers>

Now, we will try to display names of all the children under cricketers tag using the descendant XPath axis.

XpathDescendantDemo.java


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;


public class XpathDescendantDemo
{
	public static void main(String[] args) throws Exception
	{

		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document doc = documentBuilder.parse("src/cricketTeam_info.xml");

		XPathFactory xpathFactory = XPathFactory.newInstance();
		XPath xpath = xpathFactory.newXPath();

		//XPath descendant example
		XPathExpression expr = xpath.compile("/cricketers//*/name/text()");
		NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
		System.out.println("The cricketers in the team are: ");
		for(int i = 0; i < result.getLength(); i++)
		System.out.println((i+1)+"."+result.item(i).getNodeValue());

         }
}

Output:


The cricketers in the team are: 
1.MS Dhoni
2.Shikhar Dhawan
3.Virat Kohli
4.Shami
5.Zaheer Khan

The descendant axis is represented in Java XpathExpression using the // operator. The descendant axis here selected all the child nodes(represented by * operator). From the Node Set, we extract and display the value of the ‘name’ tag.

This axis can be used to select the nodes when there are not enough attributes/sub-tags to query or when all the descendants of that tag are to be selected.

Download the Eclipse project

Here, we studied about the Descendant axis in Xpath, and how we can use the same to effectively query the xml document.

Download
You can download the source code of this example here: XpathDescendentDemo.zip

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button