Build Java XPath Tutorial with Examples

View: 310    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Javascript   Fields: Other

9 point/1 review File has been tested

XPath is a syntax used to describe parts of an XML document. With XPath, you can refer to the first element, any attribute of the elements, all specific elements that contain the some text, and many other variations. An XSLT style-sheet uses XPath expressions in the match and select attributes of various elements to indicate how a document should be transformed.

Introduction

XPath is a syntax used to describe parts of an XML document. With XPath, you can refer to the first element, any attribute of the elements, all specific elements that contain the some text, and many other variations. An XSLT style-sheet uses XPath expressions in the match and select attributes of various elements to indicate how a document should be transformed. XPath can be sometimes useful while testing web services using XML for sending request and receiving response. This post is part of how to mini guides. If working with XPath, you would like to know about other best practices also.

In this post, we will see some useful ways to apply xpath to extract information from an XML file.

For next sections, I will use this XML to give examples for various facts.

<?xml version="1.0" encoding="utf-8" ?>
<inventory>
    <!--Test is test comment-->
        <book year="2000">
        <title>Snow Crash</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553380958</isbn>
        <price>14.95</price>
    </book>
    <book year="2005">
        <title>Burning Tower</title>
        <author>Larry Niven</author>
        <author>Jerry Pournelle</author>
        <publisher>Pocket</publisher>
        <isbn>0743416910</isbn>
        <price>5.99</price>
    </book>
    <book year="1995">
        <title>Zodiac</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553573862</isbn>
        <price>7.50</price>
    </book>
</inventory>

What is XPath

XPath uses language syntax much similar to what we already know. The syntax is a mix of basic programming language expressions (wild cards such as $x*6) and Unix-like path expressions (such as /inventory/author). In addition to the basic syntax, XPath provides a set of useful functions (such as count() or contains(), much similar to utility functions calls) that allow you to search for various data fragments inside the document.

XPath Data Model

XPath views an XML document as a tree of nodes. This tree is very similar to a Document Object Model (DOM) tree, so if you’re familiar with the DOM, you will easily get some understanding of how to build basic XPath expressions.

There are seven kinds of nodes in XPath data model:

  • The root node (Only one per document)
  • Element nodes
  • Attribute nodes
  • Text nodes
  • Comment nodes
  • Processing instruction nodes
  • Namespace nodes

The Root Node :

The root node is the XPath node that contains the entire document. In our example, the root node contains theelement. In an XPath expression, the root node is specified with a single slash (/).

Element Nodes

Every element in the original XML document is represented by an XPath element node.

For example in our sample xml below are element nodes.

  1. book
  2. title
  3. author
  4. publisher
  5. isbn
  6. price

Attribute Nodes

At a minimum, an element node is the parent of one attribute node for each attribute in the XML source document.These nodes are used to define the features about a particular element node.

For example in our XML fragment “year” is an attribute node.

Text Nodes

Text nodes are refreshingly simple. They contain text from an element. If the original text in the XML document contained entity or character references, they are resolved before the XPath text node is created. The text node is text, pure and simple. A text node is required to contain as much text as possible. Remember that the next or previous node of a text node can’t be another text node.

For example, all values in our XML fragment are text nodes e.g. “Snow Crash” and “Neal Stephenson”.

Comment Nodes

A comment node is also very simple—it contains some text. Every comment in the source document becomes a comment node. The text of the comment node contains everything inside the comment, except the opening <!-- and the closing -->.

For example:

<!--Test is test comment-->

Processing Instruction Nodes

processing instruction node has two partsa name (returned by the name() function) and a string value. The string value is everything after the name <?xml, including white space, but not including the ?> that closes the processing instruction.

For example:

<?xml version="1.0" encoding="utf-8"?>

Namespace Nodes

Namespace nodes are almost never used in XSLT style sheets; they exist primarily for the XSLT processor’s benefit.

Remember that the declaration of a namespace (such as xmlns:auth=”http://www.authors.net”), even though it is technically an attribute in the XML source, becomes a namespace node, not an attribute node.

 

XPath data types

In java, An XPath expression may return one of following data types:

node-set
Represents a set of nodes. The set can be empty, or it can contain any number of nodes.

node (Java support it)
Represents a single node. This can be empty, or it can contain any number of child nodes.

boolean
Represents the value true or false. Be aware that the true or false strings have no special meaning or value in XPath; see Section 4.2.1.2 in Chapter 4 for a more detailed discussion of boolean values.

Represents a floating-point number. All numbers in XPath and XSLT are implemented as floating-point numbers; the integer (or int) datatype does not exist in XPath and XSLT. Specifically, all numbers are implemented as IEEE 754 floatingpoint numbers, the same standard used by the Java float and double primitive types. In addition to ordinary numbers, there are five special values for numbers: positive and negative infinity, positive and negative zero, and NaN, the special symbol for anything that is not a number.

string
Represents zero or more characters, as defined in the XML specification. These datatypes are usually simple, and with the exception of node-sets, converting between types is usually straightforward. We won’t discuss these datatypes in any more detail here; instead, we’ll discuss datatypes and conversions as we need them to do specific tasks.

XPath Syntax

As discussed XPath uses UNIX and regex kind syntax. Lets have a look in detail:

Selecting nodes

EXPRESSION DESCRIPTION
nodename Selects all nodes with the name “nodename
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

Using predicates

 

Using predicates

Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets.
We will learn about how to use them in next section.

Reaching unknown nodes

XPath wildcards can be used to select unknown XML elements.

WILDCARD DESCRIPTION
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind

XPath Axes

 

XPath Axes

An axis defines a node-set relative to the current node. Following are axes defined by default.

AXISNAME RESULT
ancestor Selects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attribute Selects all attributes of the current node
child Selects all children of the current node
descendant Selects all descendants (children, grandchildren, etc.) of the current node
descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
following Selects everything in the document after the closing tag of the current node
following-sibling Selects all siblings after the current node
namespace Selects all namespace nodes of the current node
parent Selects the parent of the current node
preceding Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
preceding-sibling Selects all siblings before the current node
self Selects the current node
 

Playing with XPath expressions

Further, lets try to retrieve different parts of XML using Xpath expressions and above given data types.

package xml;
 
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;
import org.w3c.dom.NodeList;
 
public class XPathTest {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("sample.xml");
 
        XPathFactory xpathfactory = XPathFactory.newInstance();
        XPath xpath = xpathfactory.newXPath();
 
        System.out.println("n//1) Get book titles written after 2001");
        // 1) Get book titles written after 2001
        XPathExpression expr = xpath.compile("//book[@year>2001]/title/text()");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//2) Get book titles written before 2001");
        // 2) Get book titles written before 2001
        expr = xpath.compile("//book[@year<2001]/title/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//3) Get book titles cheaper than 8 dollars");
        // 3) Get book titles cheaper than 8 dollars
        expr = xpath.compile("//book[price<8]/title/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//4) Get book titles costlier than 8 dollars");
        // 4) Get book titles costlier than 8 dollars
        expr = xpath.compile("//book[price>8]/title/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//5) Get book titles added in first node");
        // 5) Get book titles added in first node
        expr = xpath.compile("//book[1]/title/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//6) Get book title added in last node");
        // 6) Get book title added in last node
        expr = xpath.compile("//book[last()]/title/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//7) Get all writers");
        // 7) Get all writers
        expr = xpath.compile("//book/author/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        System.out.println("n//8) Count all books titles ");
        // 8) Count all books titles
        expr = xpath.compile("count(//book/title)");
        result = expr.evaluate(doc, XPathConstants.NUMBER);
        Double count = (Double) result;
        System.out.println(count.intValue());
 
        System.out.println("n//9) Get book titles with writer name start with Neal");
        // 9) Get book titles with writer name start with Neal
        expr = xpath.compile("//book[starts-with(author,'Neal')]");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getChildNodes().item(1)// node
                                                                    // <title>
                                                                    // is on
                                                                    // first
                                                                    // index
                    .getTextContent());
        }
 
        System.out
                .println("n//10) Get book titles with writer name containing Niven");
        // 10) Get book titles with writer name containing Niven
        expr = xpath.compile("//book[contains(author,'Niven')]");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getChildNodes().item(1)// node
                                                                    // <title>
                                                                    // is on
                                                                    // first
                                                                    // index
                    .getTextContent());
        }
 
        // 11) Get book titles written by Neal Stephenson
        System.out.println("//11) Get book titles written by Neal Stephenson");
        expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
 
        // 12) Get count of book titles written by Neal Stephenson
        System.out
                .println("n//12) Get count of book titles written by Neal Stephenson");
        expr = xpath.compile("count(//book[author='Neal Stephenson'])");
        result = expr.evaluate(doc, XPathConstants.NUMBER);
        count = (Double) result;
        System.out.println(count.intValue());
 
        System.out.println("n//13) Reading comment node ");
        // 13) Reading comment node
        expr = xpath.compile("//inventory/comment()");
        result = expr.evaluate(doc, XPathConstants.STRING);
        String comment = (String) result;
        System.out.println(comment);
    }
}

On executing the above program, below the output in my system. Please verify and let me know if there is any difference.

​​

Build Java XPath Tutorial with Examples

Build Java XPath Tutorial with Examples Posted on 15-08-2016  XPath is a syntax used to describe parts of an XML document. With XPath, you can refer to the first element, any attribute of the elements, all specific elements that contain the some text, and many other variations. An XSLT style-sheet uses XPath expressions in the match and select attributes of various elements to indicate how a document should be transformed. 9/10 310

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 633    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    1.5/3 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 736    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    1.5/3 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 340    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 406    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 364    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    3/3 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 346    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 5116    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 821    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    4.5/1 review
    Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd