Home Tutorials XPath Use of "preceding" axis in XPath expression

Related Tutorials


 
 

Share on Google+Share on Google+

Use of "preceding" axis in XPath expression

Advertisement
In this part of tutorial we are going to describe you the use of "preceding" axis in XPath expression. "preceding" axis will select each and every element or node before the given context node.

Use of "preceding" axis in XPath expression

     

In this part of tutorial we are going to describe you the use of "preceding" axis in XPath expression. "preceding" axis will select each and every element or node before the given 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.

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>

We have put that persons.xml file in that current working directory. Now we have declared a class XPathPreceding  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. 

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();

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

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

XPath expression "//person/preceding::*" will select all elements that are before the start of the context node.
Here is the example code for XPathPreceding.java as follows:


XPathPreceding.java

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

public class XPathPreceding {

  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("//person/preceding::*");
  // Selecting person element's preceding elements name 
  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("//person/preceding::*/name/text()");
  // Selecting all person element's precding elements values 
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.print(nodes.item(i).getNodeValue()+",  ")
  }  
  }
}


Output:



Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: September 29, 2008

Related Tutorials

Discuss: Use of "preceding" axis in XPath expression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com