[an error occurred while processing this directive]

Use of "following" in XPath


In this section you will learn how to use "following" in your XPath expression. "following" axis in XPath expression will select each and every node or element following this current node closing point.

In this example we have created an XML file "persons.xml" as in our previous section, which is necessary to execute XPath query on to it. This "persons.xml" contains information related to name, age, gender of different persons.

Here is the full source code for persons.xml file as follows :


<?xml version="1.0" ?>
    <person id="1">
     <person id="2">
    <person id="3">

    <!-- more persons... -->

Now we have declared a class XPathFollowing class and in this class we are parsing the XML file with JAXP. First of all we need to load the document into DOM Document object. For downloading document we have created a new instance of DocumentBuilderFactory and then this instance is further going to be used in creating an object of DocumentBuilder. "setNamespaceAware()" tells that the parser produced by this code will provide full support for XML namespaces. We have put that persons.xml file in that current working directory.

    DocumentBuilderFactory domFactory = 

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("persons.xml");

Above lines of code parses "persons.xml" file and creates a Document object. Next we have created XPath object with the use of XPathFactory.

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

Expression "//person/following::name/" will select all element's names following the name element. "//person/following::name/text()" will show the text value of the following name node's.Here is the example code for XPathFollowing.java :


import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class XPathFollowing {

  public static void main(String[] args
   throws ParserConfigurationException, SAXException, 
          IOException, XPathExpressionException {

    DocumentBuilderFactory domFactory = 
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("persons.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();  
    XPathExpression expr = xpath.compile("//person/following::name");
       // Selecting all Node's name of 
       // elements following name and preceding element person

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeListresult;
    for (int i = 0;i < nodes.getLength();i++) {
    expr = xpath.compile("//person/following::name/text()");
       // Selecting all element's value following 
       // element name and preceding element person 

    result = expr.evaluate(doc, XPathConstants.NODESET);
    nodes = (NodeListresult;
    for (int i = 0; i < nodes.getLength(); i++) {
     System.out.print(nodes.item(i).getNodeValue()+",  ")


Download Source Code


[an error occurred while processing this directive]