Home » Core Java » xml » XPath » XPath get attribute value 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 get attribute value example

Continuing on our previous example on how to search for a text using XPath, we shall demonstrate how to get an attribute value from an XPath Node.

As we already know, the @ attribute is used to denote an attribute in the XPathExpression. So we shall use extract the attribute value by specifying the node selector in the expression.

In the example, the name of the cricketer is known. We have to determine the handedness of the cricketer, which is mentioned in the type attribute of the XPath.

 
 

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>

ExtractXPathAttribute.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.NodeList;


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

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

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

				// get the type attribute of cricketer with name = 'Shami'
				XPathExpression expr = xpath.compile("//cricketer[name='Shami']/@type");
				String names = (String) expr.evaluate(document, XPathConstants.STRING);
				System.out.println("Righty attribute is : " + names);

				//get the type attribute of cricketers with role = 'Bowler'
				expr = xpath.compile("//cricketer[role='Bowler']/@type");
				NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
				for (int i = 0; i < nodes.getLength(); i++)
						System.out.println("Righty attribute is : " + nodes.item(i).getNodeValue());

		}
}

OUTPUT:


The type of cricketer with name Shami is : righty
Bowler Attribute is : lefty
Bowler Attribute is : righty

In the first case we query the XPath using the name of the cricketer and the return type is XPathConstants.STRING. In the second case, we query the XML document for all the cricketers in the role of "BOWLER" for their handedness type. We extract the XPathConstants.NODESET and iterate over it to get the results.

Conclusion

Here we studied, how we can extract the attribute value from an XPath node.

Download
You can download the source code of this example here: ExtractXPathAttributeDemo.zip
(No Ratings Yet)
Start the discussion 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

 

Leave a Reply

avatar
  Subscribe  
Notify of