XPath

XPath substring example

Photo of Chandan SinghChandan SinghMarch 17th, 2015Last Updated: March 17th, 2015
0 2,028 2 minutes read

In this example, we will try to see how we can use thesub-string method in theXPath for our use-cases.

Thesub-string method is used to search for the sub-string in the beginning or the end or anywhere in theXPath node. It can also be used to extract the part of the sub-string from the string by specifying the beginning index and end index.

 
 
 
 
 

TheXPath provides following methods for this :

 

  • substring-before(String target, String tosearch)
  • substring-after(String target, String tosearch)
  • substring(String target, int startingindex, int length)

We will look at all the methods in brief and see how to use each of them.

cricketTeam_info.java:

<?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>

1. substring(String target, int startingindex, int length)

Substring(target, startindex, length, ) method returns the sub-string of the target string from the start-index to thelength specified. If the length argument is not provided, it returns the string from thestart-index specified to the last character of the target string.

XPathExpression expr = xpath.compile("substring(//cricketer[name='MS Dhoni']/position,'1','4')");String substr = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring of Wicket is : " + substr );

Output:

The substring  of Wicket is : Wick

The target string here is ‘wicket-keeper‘. By passing1,4 we are extracting the characters from 1st to 4th position of the target string.

NOTE:
Unlike in Java, the XPath string index starts with 1.

2. substring-before(String target, String tosearch)

Thesubstring-before is used to extract the substring of the target occurring before an occurrence of the string passed as toSearch argument. Lets look at an example to understand this :

expr = xpath.compile("substring-before(//cricketer[name='MS Dhoni']/position,'-Keeper')");String substrbefore = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring before Keeper is : " + substrbefore);

Output:

The substring before Keeper is : Wicket

The string occurring prior to“-Keeper” in“Wicket-Keeper” is returned by thesubstring-beforemethod.

3. substring-after(String target, String tosearch)

This method works exactly opposite of thesubstring-before method. It returns the substring after the tosearch string till the end of the target string.

expr = xpath.compile("substring-after(//cricketer[name='MS Dhoni']/position,'Wicket-')");String substrafter = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring after Keeper is : " + substrafter);

Output:

The substring after Keeper is : Keeper

Thesubstring-after returns the substring after the “Wicket“, which is “keeper“.

XpathSubStringDemo.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 subtring exampleXPathExpression expr = xpath.compile("substring(//cricketer[name='MS Dhoni']/position,'1','4')");String substr = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring before Keeper is : " + substr);// XPath subtring-before exampleexpr = xpath.compile("substring-before(//cricketer[name='MS Dhoni']/position,'-Keeper')");String substrbefore = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring before Keeper is : " + substrbefore);// XPath subtring-after exampleexpr = xpath.compile("substring-after(//cricketer[name='MS Dhoni']/position,'Wicket-')");String substrafter = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring after Keeper is : " + substrafter);}}

4. Conclusion:

Here we studied the sub-string method and how to use them.

Download
You can download the source code of this example here:XpathSubStringDemo.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 17th, 2015Last Updated: March 17th, 2015
0 2,028 2 minutes 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.