Share on Google+Share on Google+

Use of "descendant" in XPath expression

In this section of XPath in Java tutorial you will learn use of descendant in XPath expression.

Use of "descendant" in XPath expression


In this section of XPath in Java tutorial you will learn use of descendant in XPath expression. "descendant" will select descendant elements of the context element node. "descendant" are child or sub-child of a child element.

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 XPathDescendant  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. 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();

In XPathDescendant class XPath expression "/information/descendant::*/" will select information element's descendant names and "//descendant::person/*/text()" will select all descendants of element person's value.
Here is the example code for as follows:

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

public class XPathDescendant {

  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("/information/descendant::*");
  // Selecting information element descendant's name 
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
    expr = xpath.compile("//descendant::person/*/text()");
  // Selecting all person element descendant's value 
  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




Posted on: September 29, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Use of "descendant" in XPath expression  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image