XPath

XPath parent node example

In the previous example, we introduced the dependant axis specifier. In this example, we shall talk about the ancestor axis in XPath Expression.

The ancestor tag is used to select the ancestor of the current node and the ancestor of the ancestor and so on in context of the current node.

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

Consider the XML file below for our example. The File has a number of nodes under the 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 find out the ancestor of the <cricketer> Node using the ancestor axis in XPath. The ancestor axis is represented using the ancestor::.

XpathAncestorDemo.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.Node;


public class XpathAncestorDemo
{
	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 ancestor axis example
		XPathExpression expr = xpath.compile("//cricketer/ancestor::*");
		Node ancestor = (Node) expr.evaluate(doc, XPathConstants.NODE);
		System.out.println("The cricketers in the team are: ");
		System.out.println("The ancestor element is "+ancestor.getNodeName());

	}
}

Output:


The ancestor element is cricketers

As you can see, the XPathExpression returns a Nodelist object. This object contains a list of Nodes (i.e. ancestor of current Node, ancestor of ancestor and so on… ).

We may also choose access the child tags of the ancestor using the ancestor axis. Lets’s take a look at an example:


//Acessing child tags of ancestor
expr = xpath.compile("//ancestor::*/name/text()");
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
System.out.println("The name elements are ");
for(int i = 0; i < nodeList.getLength(); i++)
{
	System.out.println((i+1)+". "+nodeList.item(i).getNodeValue());
}

Output:


The name elements are 
1. MS Dhoni
2. Shikhar Dhawan
3. Virat Kohli
4. Shami
5. Zaheer Khan

We can access any tag of the ancestor in the same fashion.

Download the Source Code

Here, we studied about the Ancestor 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: XpathAncestorDemo.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