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 :
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.
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 expression "
will select all elements that are before the
start of the context node.
Here is the example code for XPathPreceding.java as follows:
Ask Questions? Discuss: Use of "preceding" axis in XPath expression
Post your Comment
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.