简体   繁体   中英

java xpath how to get all data different of specific node

I would like to get all nodes different of node car.

XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate("//car", document, XPathConstants.NODESET);

How to set something like :

NodeList nodes = (NodeList) xpath.evaluate(!"//car", document, XPathConstants.NODESET);

Adding an exclamation mark in front of the XPath string does not work. Indeed, for Java, the XPath expression is just a String : its content will not be interpreted by Java itself, but by the XPath library you are using.

Therefore, the only solution here is to modify the XPath expression inside the String .

You can do it as follows:

NodeList nodes = (NodeList) xpath.evaluate("//*[name() != 'car']", document, XPathConstants.NODESET);

Explanation: //*[name() != 'car' means : find all nodes in the document whose name is not 'car'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM