Reputation: 3152
I'm using this code, but I'm not sure what should the XPath expression look like.
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("XXXX");
NodeList nodes = (NodeList) expression.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
Let's say there's a tree:
<animal>
<big>
<elephant>
<white_elephant>
</white_elephant>
</elephant>
</big>
<small>
</small>
</animal>
I would like to get every descendant of node big: (elephant, white_elephant) + big itself
Upvotes: 0
Views: 1087
Reputation: 4349
You can use the descendant-or-self
-axis:
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("//big/descendant-or-self::*");
NodeList nodes = (NodeList) expression.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
Wikipedia is a good source to get a overview over different axis: http://en.wikipedia.org/wiki/XPath#Axis_specifiers
Edit: As Jens Erat mentions in the comments, you can shorten /descendant-or-self::
to //
- it's just an abbreviation:
XPathExpression expression = xpath.compile("//big//*");
Upvotes: 3