Home » Core Java » xml » XPath » XPath contains text example

About Chandan Singh

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.

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
(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Naseem Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Naseem
Guest
Naseem

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.