Using following-sibling in XPath
In this part of tutorial we are going to describe you how one can use "following-sibling" axis in to the XPath expression. "following-sibling" has some other working rather than "following" axis because it selects all siblings following the context node.
In this example also we have created an XML file "persons.xml" as in our previous section, which is necessary to execute XPath query or expression 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" ?> |
Now we have declared a class XPathFollowSibling 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. We have put that
persons.xml file in that current working directory. 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.
DocumentBuilderFactory domFactory = |
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(); |
"setNamespaceAware()" tells that the
parser produced by this code will provide full support for XML namespaces.
Expression "//following-sibling::*" will
select all the following sibling of root node "information".
Here is the example code for XPathFollowSibling.java
as follows:
XPathFollowSibling.java
import org.w3c.dom.*;
|