Home » Core Java » xml » XPath » XPath innertext selection example

About Mohammad Meraj Zia

Mohammad Meraj Zia
I did my Engineering in Information Technology from IET, Lucknow, India. Currently doing MSc in Information Technology from Derby University. I have worked in Java/J2EE domain for the last 10 years. Have good understanding of Payment and Finance domains.

XPath innertext selection example

In this example we will learn how to select a node with a given inner text using XPath. We will start with the introduction of XPath – what it is and how to use it, then we will see the working model of inner-text selection. Tools and technologies used in this example are Java 1.6, Intellij.

1. Overview

An XPath expression specifies a pattern that selects a set of XML nodes. XSLT templates then use those patterns when applying transformations. The nodes in an XPath expression refer to more than just elements. They also refer to text and attributes, among other things. In fact, the XPath specification defines an abstract document model that defines seven kinds of nodes:

  • Root
  • Element
  • Text
  • Attribute
  • Comment
  • Processing instruction
  • Namespace

XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value. XPath is a W3C-defined language and an official W3C recommendation; the W3C hosts the XML Path Language (XPath) Version 1.0 specification.

2. XPath Expressions

An XPath expression is composed of a location path and one or more optional predicates. Expressions may also include XPath variables.

The following is an example of a simple XPath expression:
/Country/City
This example would select the <City> element in an XML document.

3. Project Structure

Below is the project structure used in this example. Please note that I have used maven to build the project but for this example you don’t need it.

Figure 1. Project Structure

Figure 1. Project Structure

4. XLM file

Below is the xml file representing Users. Each user has got a unique UserId, name, address, sex, Date Of Birth and email.

user.xml

<?xml version="1.0" encoding="UTF-8"?>
<Users>
    <User>
        <UserId>12345</UserId>
        <Name>
            <FirstName>Maden</FirstName>
            <MiddleName>Over</MiddleName>
            <Surname>Gulliver</Surname>
        </Name>
        <Address>
            <FirstLineOfAddress>Oxford Street</FirstLineOfAddress>
            <SecondLineOfAddress></SecondLineOfAddress>
            <City>London</City>
            <County>Middlesex</County>
            <Country>United Kingdom</Country>
        </Address>
        <Sex>Male</Sex>
        <DateOfBirth>01/02/1967</DateOfBirth>
        <Email></Email>
    </User>
    <User>
        <UserId>56789</UserId>
        <Name>
            <FirstName>Tom</FirstName>
            <MiddleName>Cruise</MiddleName>
            <Surname>Kidman</Surname>
        </Name>
        <Address>
            <FirstLineOfAddress>Reagent Street</FirstLineOfAddress>
            <SecondLineOfAddress></SecondLineOfAddress>
            <City>New York</City>
            <County></County>
            <Country>United States</Country>
        </Address>
        <Sex>Female</Sex>
        <DateOfBirth>02/03/1978</DateOfBirth>
        <Email></Email>
    </User>
    <User>
        <UserId>72638</UserId>
        <Name>
            <FirstName>Amitabh</FirstName>
            <MiddleName></MiddleName>
            <Surname>Bachchan</Surname>
        </Name>
        <Address>
            <FirstLineOfAddress>Panama Street</FirstLineOfAddress>
            <SecondLineOfAddress></SecondLineOfAddress>
            <City>Mumbai</City>
            <County></County>
            <Country>India</Country>
        </Address>
        <Sex>Male</Sex>
        <DateOfBirth>05/04/1999</DateOfBirth>
        <Email>amitabh.bachchan@asv.com</Email>
    </User>
</Users>

5. Java file

Below is the java file which will use XPath to extract information from the User.xml.

ExampleXPathInnertext.java

package com.javacodegeeks;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;

/**
 * Example code for XPath Inner-text selection
 * @author Java Code Geeks
 */
public class ExampleXPathInnertext {

    private static DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    private static DocumentBuilder builder = null;
    private static XPath xPath = null;
    private static Document document = null;

    public static void main(String q[]) {
        try {
            builder = builderFactory.newDocumentBuilder();
            document = builder.parse(ExampleXPathInnertext.class.getResourceAsStream("/user.xml"));
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        xPath =  XPathFactory.newInstance().newXPath();
        findParentForGivenInnertext("Over");
        findChildForGivenInnerText("Over");
        findAllChildNodes("Male");
    }

    /**
     * Select an element that has a child with a particular inner-text
     */
    private static void findParentForGivenInnertext(String innerText) {
        try {
            String expression = "//Name[MiddleName='" + innerText + "']";
            NodeList nl = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
            System.out.println("######### Output for expression: " + expression + " ########");
            System.out.println(nl.item(0).getTextContent());
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Select a child with a particular inner-text
     */
    private static void findChildForGivenInnerText(String innerText) {
        try {
            String expression = "//Name/MiddleName[" + "text" + "() = '" + innerText + "']";
            NodeList nl = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
            System.out.println("######### Output for expression: " + expression + " ########");
            System.out.println(nl.item(0).getTextContent());
            expression = "//Name/MiddleName[. = '" + innerText + "']";
            NodeList n2 = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
            System.out.println("######### Output for expression: " + expression + " ########");
            System.out.println(n2.item(0).getTextContent());
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Find all child nodes of "User" where any "Sex" of "User" has $innerText as inner text.
     */
    private static void findAllChildNodes(String innerText) {
        try {
            String expression = "//User[descendant::Sex[" + "text" + "()='" + innerText + "']]";
            NodeList nl = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
            System.out.println("######### Output for expression: " + expression + " ########");
            System.out.println(nl.item(0).getTextContent());
            System.out.println(nl.item(1).getTextContent());
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

}

6. Output

######### Output for expression: //Name[MiddleName='Over'] ########

Maden
Over
Gulliver

######### Output for expression: //Name/MiddleName ########
Over
######### Output for expression: //Name/MiddleName[. = 'Over'] ########
Over
######### Output for expression: //User[descendant::Sex] ########

12345

Maden
Over
Gulliver


Oxford Street

London
Middlesex
United Kingdom

Male
01/02/1967
72638

Amitabh

Bachchan


Panama Street

Mumbai

India

Male
05/04/1999
amitabh.bachchan@asv.com

7. Download the source code

This was an example to show how to select an element given the inner-text using XPath.

Download
You can download the full source code of this example here : XPath Innertext Example
(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