XPath

XPath contains text example

In this example, we shall study how to search for a particular text using XPath in Java.

What is XPath?

XPath is a query language used to define/select parts of an XML Document using variety of criteria. Java provides all the utility classes for using XPath in the javax.xml.xpath package.

XPath uses pattern to filter out the matching nodes. The XPathExpression.evaluate() method is used for evaluation of the expressions. The method returns as output any one of the following XPath Constants:

  1. XPathConstants.STRING
  2. XPathConstants.NUMBER
  3. XPathConstants.BOOLEAN
  4. XPathConstants.NODE
  5. XPathConstants.NODESET

The type-names are self explanatory. Node returns a single matching XPathConstants.Node. A XPathConstants.Nodeset returns a set of Nodes.

We will look at the examples and see how we can make use of XPath to query an XML document and extract the information from it.

Consider a sample xml file below:

cricketTeam_info.xml:

<?xml version="1.0" encoding="UTF-8"?>
<cricketers>
	<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>

We will now try to extract data from this xml file using a variety of criteria.

XpathDemo.java:

package com.jcg;


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;
import org.w3c.dom.NodeList;


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

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

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

				// get the names of Right-Handed Players
				XPathExpression expr = xpath.compile("//cricketer[@type='righty']/name/text()");
				NodeList names = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
				System.out.println("The List of all Right-Handed Batsmen");
				for (int i = 0; i < names.getLength(); i++)
				{
						System.out.println(names.item(i).getNodeValue());
				}
				System.out.println();
				
				// get the name of first cricketer
				expr = xpath.compile("/cricketers/cricketer[1]/name/text()");
				Node name = (Node) expr.evaluate(document, XPathConstants.NODE);
				System.out.println("First Player is "+name.getNodeValue());
				System.out.println();

				// get the list of bowlers
				expr = xpath.compile("/cricketers/cricketer[role='Bowler']/name/text()");
				NodeList bowlers = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
				System.out.println("List of all Bowlers in the Team");
				for (int i = 0; i < bowlers.getLength(); i++)
						System.out.println(bowlers.item(i).getNodeValue());
				
				//get the count of all players in the team
				expr = xpath.compile("count(/cricketers/cricketer)");
				int teamSize = ((Number) expr.evaluate(doc, XPathConstants.NUMBER)).intValue();
				System.out.println("List of all Bowlers in the Team");
				System.out.println(teamSize);

                //get the list of cricketer names containing 'ha'(case-sensitive!) 
				expr = xpath.compile("//cricketer[contains(name,'ha')]/name/text()");
				NodeList teammates = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
				System.out.println("List of all players in the Team with 'Sh' in their name ");
				for (int i = 0; i < teammates.getLength(); i++)
						System.out.println(teammates.item(i).getNodeValue());

                //get the list of cricketer names starting with 'Sh' 
				expr = xpath.compile("//cricketer[starts-with(name,'Sh')]/name/text()");
				teammates = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
				System.out.println("List of all players in the Team with names starting with 'Sh' ");
				for (int i = 0; i < teammates.getLength(); i++)
						System.out.println(teammates.item(i).getNodeValue());
		}
}

OUTPUT:


The List of all Right-Handed Batsmen
MS Dhoni
Virat Kohli
Shami

First Player is MS Dhoni

List of all Bowlers in the Team
Shami
Zaheer Khan
List of all Bowlers in the Team
5
List of all players in the Team with 'HA' in their name 
Shikhar Dhawan
Shami
Zaheer Khan
List of all players in the Team with names starting with 'Sh' 
Shikhar Dhawan
Shami

We get an instance of XPath from the XPathFactory using the XPathFactory.newXPath() method. Then we proceed to create an XPathExpression object using the expression we need. Finally, we evaluate the expression on the XML document to extract the requisite data from the file.

Summary

In this example we created a small application used to find a particular node/text in XML using XPath.

Download
You can download the source code of this example here: XPathDemo.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.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Naseem
Naseem
6 years ago

Thanks. How can we handle single and double quotes in this result? -> xpath.compile(“/cricketers/cricketer[1]/name/text()”);

With this syntax, it replaces single and double quotes with junk.

Back to top button