Use of "parent" in XPath expression

In our previous section of tutorials you have studied how to use child axis in XPath expression. Now this section will describe the use of "parent" axis.

Use of "parent" in XPath expression

Use of "parent" in XPath expression

     

In our previous section of tutorials you have studied how to use child axis in XPath expression . Now this section will describe the use of "parent" axis. "parent" axis consists of parent of the context node. 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. Our XML file persons.xml is as same as in our previous examples.

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

persons.xml

<?xml version="1.0" ?>
<information>
  <person id="1">
  <name>Deep</name>
  <age>34</age>
  <gender>Male</gender>
  </person>
 
 <person id="2">
  <name>Kumar</name>
  <age>24</age>
  <gender>Male</gender>
  </person>
 
  <person id="3">
  <name>Deepali</name>
  <age>19</age>
  <gender>Female</gender>
  </person>

  <!-- more persons... -->
</information>

Now we have declared a class XPathParentAxis  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. DocumentBiuldFactory's static method newInstance() is used to create a new instance of DocumentBuilderFactory. Instance of DocumentBuilderFactory is used to create an object of DocumentBuilder.

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();

  domFactory.setNamespaceAware(true); 
  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 our example XPath expression "//parent::*" will select all elements which are parent of context node. 
Here is the full example code of XPathParentAxis.java as follows:

XPathParentAxis.java 

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

public class XPathParentAxis {

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

  DocumentBuilderFactory domFactory =
    DocumentBuilderFactory.newInstance
();
  domFactory.setNamespaceAware(true)
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("persons.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();
  
    XPathExpression expr = xpath.compile("//parent::*");
  // Selecting all element's Parent Axis 
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }
    expr = xpath.compile("//parent::name/text()");
 // Selecting all name element's Parent Axis 
    result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.print(nodes.item(i).getNodeValue()+",  ")
  }
    expr = xpath.compile("//parent::age/text()");
 // Selecting all age element's Parent Axis 
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.print(nodes.item(i).getNodeValue()+",  ")
  }  
  expr = xpath.compile("//parent::gender/text()");
 //Selecting all gender elemenet's Parent Axis 
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.print(nodes.item(i).getNodeValue()+",  ")
  }  
  }
}

Output:

To run this example follow these stated steps:

  • create persons.xml file
  • create and save XPathParentAxis.java
  • compile and execute XPathParentAxis class file and you will get following console output.


Download Source Code