In this section of XPath in Java tutorial you will learn use of descendant in XPath expression.
Use of "descendant" in XPath expression
In this section of XPath in Java tutorial you will
learn use of descendant in XPath expression. "descendant" will
select descendant elements of the context element node. "descendant"
are child or sub-child of a child element.
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 :
Now we have declared a class XPathDescendant 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.
In XPathDescendant class XPath expression "/information/descendant::*/"
will select information element's descendant names and "//descendant::person/*/text()"
will select all descendants of element person's value.
Here is the example code for XPathDescendant.java