Java XPath Example: concat

We studied about Xpath normalize-space() method in the previous example. In this example, we will study how the concat(String s1, String s2, String... s3) method works.

The XPath-concat method works similar to the String#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 more XPathExpression to evaluate. The output from the XPathExpressionis then concatenated and the resulting string is returned.

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

Consider the XML file below:


01 <?xml version="1.0" encoding="UTF-8"?>
03 <cricketers>
04     <cricketer type="righty">
05         <name>MS Dhoni</name>
06         <role>Captain</role>
07         <position>Wicket-Keeper</position>
08     </cricketer>
09     <cricketer type="lefty">
10         <name>Shikhar Dhawan</name>
11         <role>Batsman</role>
12         <position>Point</position>
13     </cricketer>
14     <cricketer type="righty">
15         <name>Virat Kohli</name>
16         <role>Batsman</role>
17         <position>cover</position>
18     </cricketer>
19     <cricketer type="righty">
20         <name>Shami</name>
21         <role>Bowler</role>
22         <position>SquareLeg</position>
23     </cricketer>
24     <cricketer type="lefty">
25         <name>Zaheer Khan</name>
26         <role>Bowler</role>
27         <position>FineLeg</position>
28     </cricketer>
29 </cricketers>

Now we will concat the attributes of two cricketers using the XPath concat method.

01 import javax.xml.parsers.DocumentBuilder;
02 import javax.xml.parsers.DocumentBuilderFactory;
03 import javax.xml.xpath.XPath;
04 import javax.xml.xpath.XPathConstants;
05 import javax.xml.xpath.XPathExpression;
06 import javax.xml.xpath.XPathFactory;
08 import org.w3c.dom.Document;
11 public class XpathSubStringDemo
12 {
13         public static void main(String[] args) throws Exception
14         {
16                 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
17                 documentBuilderFactory.setNamespaceAware(true);
18                 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
19                 Document doc = documentBuilder.parse("src/cricketTeam_info.xml");
21                 XPathFactory xpathFactory = XPathFactory.newInstance();
22                 XPath xpath = xpathFactory.newXPath();
24                 //XPath concat example
25                 XPathExpression expr = xpath.compile("concat(//cricketer[name='Shami']/@type,//cricketer[name='Zaheer Khan']/@type)");
26                 String combination = (String) expr.evaluate(doc, XPathConstants.STRING);
27                 System.out.println("The concat result is : " + combination);
30         }
31 }


1 The concat result is : rightylefty

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

We can also concat static string with output of an XpathExpression or two static strings themselves. The snippet below demonstrates this:

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

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

1 The handed-ness of ZKhan is : lefty

Download the source code

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

You can download the source code of this example here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s