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 :
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.