XPath

XPath concat Example

Photo of Chandan SinghChandan SinghMarch 30th, 2015Last Updated: April 1st, 2015
0 1,984 1 minute read

We studied aboutXpath normalize-space() method in theprevious example. In this example, we will study how theconcat(String s1, String s2, String... s3) method works.

TheXPath-concat method works similar to theString#concat method. It joins two or more strings into a single string. The argument strings maybe two or more static strings and/or they may be two or moreXPathExpression to evaluate. The output from theXPathExpressionis then concatenated and the resulting string is returned.

We will look at an example to see how theconcat method works in practice:

Consider theXML file below:

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 willconcat the attributes of two cricketers using theXPath concat method.

XpathNormalizeSpaceDemo.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;public class XpathSubStringDemo{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 concat exampleXPathExpression expr = xpath.compile("concat(//cricketer[name='Shami']/@type,//cricketer[name='Zaheer Khan']/@type)");String combination = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The concat result is : " + combination);}}

Output:

The concat result is : rightylefty

As you can see in the output, the result of the two Xpath expressions are concatenated producing output asrightylefty.

We can alsoconcat static string with output of anXpathExpression or two static strings themselves. The snippet below demonstrates this:

XPathExpression expr = xpath.compile("concat('The handed-ness of ZKhan is : ',//cricketer[name='Zaheer Khan']/@type)");String result = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println(result);

In the above code-snippet, the concat method has:

 The handed-ness of ZKhan is : lefty

Download the source code

Here we studied how the Xpathconcat(String s1, String s2, String... s3) method works.

Download
You can download the source code of this example here:XPathConcatDemo.zip
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Chandan SinghChandan SinghMarch 30th, 2015Last Updated: April 1st, 2015
0 1,984 1 minute read
Photo of 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.

Related Articles

Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.