XPath

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

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
Inline Feedbacks
View all comments
Back to top button