Home » Core Java » xml » XPath » XPath Like Operator 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 Like Operator Example

In this example we will learn about XPath like operator. We will see how we can perform a ‘like’ operation using XPath. We will make use of IDEA IntelliJ to show some examples.

1. Introduction

XPath is a W3C recommendation. It stands for XML Path Language and uses “path like” syntax to identify and navigate nodes in an XML document. XPath is a major element in the XSLT standard. XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the path expressions you use with traditional computer file systems.
XPath includes over 200 built-in functions. There are functions for string values, numeric values, booleans, date and time comparison, node manipulation, sequence manipulation, and much more. Today XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

2. XPath Like Operator

There are various functions available which can be used to mimic the same behaviour as the ‘like’ operator. Below we will see some of those:

2.1 starts-with(string1, string2)

The starts-with checks whether the first string starts with the second string and returns true or false. The first parameter of the starts-with XPath function is used to specify the source node or string against which the comparison is to be executed. The second parameter is the pattern string that specifies the character or character sequence that is to be used in the comparison. It is important to remember that the pattern string that is supplied as the second parameter of the starts-with function is case sensitive.
For example starts-with(Hello, World) will return false while starts-with(Hello World, Hello) will return true.

2.2 contains(string1, string2)

The contains() function determines whether the first argument string contains the second argument string and returns boolean true or false.  If an argument is not of type string, it is first converted to a string using the string() function and then the result of that conversion is evaluated. Please note that this function is also case-sensitive.
XPath 2.0 also supports the regular expressions which can be used for the queries involving the like operator.

2.3 Pattern matching

The three functions described below make use of a regular expression syntax for pattern matching:
fn:matches: Returns an xs:boolean value that indicates whether the value of the first argument is matched by the regular expression that is the value of the second argument.
fn:replace: Returns the value of the first argument with every substring matched by the regular expression that is the value of the second argument replaced by the replacement string that is the value of the third argument.
fn:tokenize: Returns a sequence of one or more xs:strings whose values are substrings of the value of the first argument separated by substrings that match the regular expression that is the value of the second argument.
We can use the matches() function for our case.

3. Example

In this section we will show the working example of the things we have discussed in the previous section. For this example we will make use of IDEA IntelliJ but you can use any other IDE of your choice.

Open IntelliJ and click on File=>New=>Project. Choose Java and click Next. Click Next on the next pop-up. Give the name of the project and the location and click Finish.

Figure 1. Java Project

IntelliJ will create a basic structure for your project.

Now we will see how to create a new java package. To create a new package right click on the ‘src’ folder and choose New=>Package.

Figure 2. New Package

Give the package name (com.javacodegeeks) and click OK. IntelliJ will create a package for you. Now right click on the package and choose New=>Java Class. Give the class name and click OK. IntelliJ will create a class for you in the specified package.

For simplicity we will create an XML file at the same location where the java file exist. Ideally you should keep your static files in different location. To create an XML file right click on the package and choose New => File. Give the file name and click OK. We will create some test entries as below:

test.xml

<books>
    <book id="123456">
        <title>Title 1</title>
        <author>Author 1</author>
        <publisher>Publisher 1</publisher>
        <isbn>ISBN1</isbn>
        <cost>56.98</cost>
    </book>
    <book id="452234">
        <title>Title 2</title>
        <author>Author 2</author>
        <publisher>United Nation 2</publisher>
        <isbn>ISBN2</isbn>
        <cost>21.32</cost>
    </book>
    <book id="897855">
        <title>Title 3</title>
        <author>Author 3</author>
        <publisher>Publisher 3</publisher>
        <isbn>ISBN3</isbn>
        <cost>107.90</cost>
    </book>
</books>

Now we will see the java code required to perform the XPath query. First we need to get an instance of the javax.xml.parsers.DocumentBuilderFactory by calling the newInstance() method:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

A DocumentBuilderFactory defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. Then we will get the DocumentBuilder object by calling the newDocumentBuilder() method on the factory object:

builder = builderFactory.newDocumentBuilder();

Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.
Now we will parse the xml file by calling the parse() method on the builder object and passing the xml path to it:

Document document = builder.parse(this.getClass().getResourceAsStream("/com/javacodegeeks/books.xml"));

After this we will create an XPath object by calling the newInstance() method on the XPathFactory

XPath xPath =  XPathFactory.newInstance().newXPath();

The code for the class is shown below:

XPathLikeOperator.java

package com.javacodegeeks;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
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.FileNotFoundException;
import java.io.IOException;

public class XPathLikeOperator {

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;

    public static void main(String[] args) {
        XPathLikeOperator obj = new XPathLikeOperator();
        obj.execute();
    }

    public void execute() {
        try {
            builder = builderFactory.newDocumentBuilder();
            Document document = builder.parse(this.getClass().getResourceAsStream("/com/javacodegeeks/books.xml"));
            XPath xPath =  XPathFactory.newInstance().newXPath();
            String expression1 = "//book/publisher[starts-with(.,'United')]";
            NodeList nl = (NodeList) xPath.compile(expression1).evaluate(document, XPathConstants.NODESET);
            for(int i=0; i < nl.getLength() ; i++) {
                Node node = nl.item(i);
                System.out.println(node.getTextContent());
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }
}

4. Conclusion

In this article we discussed about XPath and how we can use various functions available to perform a ‘like’ query. We also discussed about Pattern matching and how to use the available functions to perform actions on the data. We also discussed about creating a java project using IntelliJ and writing the code to parse the xml and to perform XPath queries.

(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