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
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//*");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.