简体   繁体   中英

Java: How to execute an XPath query on a node

So I'm reading from an XML file with many layers of nesting in Java using xPath.

At the moment I have a method that takes the path to XML file and a xpath query as parameters, and returns a NodeIterator.

Then I iterate through those node, and for some of the nodes (if their name matches) I need to execute another query on them and get a NodeIterator of their children etc

Is it possible to have a function with 2 parameters, one an already existing Node and the other an xPath query to execute on that Node?

So replacing: NodeIterator ni = XPathAPI.selectNodeIterator(document,xpathQuery); With some like : NodeIterator ni2 = xPathAPI.selectNodeIterator(parentNode, query);

I've searched on the internet and I can't find any examples, and I'm not sure what the syntax to do the above would be, or if it's even possible?

Many thanks in advance :)

Presumably your XPathAPI class is the Apache/Xalan org.apache.xpath.XPathAPI?

In that case, what's wrong with

static NodeIterator selectNodeIterator(Node contextNode, java.lang.String str) 

It seems to do exactly what you want.

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