找到你要的答案

Q:Java XPath returns single result instead of NodeSet

Q:java的XPath返回单结果而不是节点集

I am trying to create an XPath expression in Java (8, default XPath implementation). I am doing the following:

Object res = xpath.evaluate("(//*[local-name()='PartyId'])", requestDom, XPathConstants.NODESET);

I have multiple PartyId nodes in the document at the same level, because it's parent is repeating. I got my result, but only a single node. (the first).

Side info: if I write [$k] at the end of the expression, like [1], or [2], I got my elements, but I need all of them. :(

However, if I am testing the very same XPath for example at http://www.freeformatter.com/xpath-tester.html I get multiple results which is the expected result. Any ideas?

p.s. I tried to put Saxon on the classpath but it completely breaks my application (Spring-Boot WS).

Thanks a lot!

UPDATE I failed to correctly check the result and it was absolutely correct.

我试图创建java中的XPath表达式(8,默认的XPath实现)。我在做以下:

Object res = xpath.evaluate("(//*[local-name()='PartyId'])", requestDom, XPathConstants.NODESET);

我有多partyid文件中的节点在同一水平,因为它的母公司是重复。我得到了我的结果,但只有一个节点。(第一)。

侧信息:如果我写[ $ ]在表达式的结尾,如[ 1 ],或[ 2 ],我得到了我的元素,但我需要他们所有。:(

However, if I am testing the very same XPath for example at http://www.freeformatter.com/xpath-tester.html I get multiple results which is the expected result. Any ideas?

我试图把撒克逊在classpath但它完全打破了我的申请(春季启动WS)。

谢谢!

UPDATE I failed to correctly check the result and it was absolutely correct.

answer1: 回答1:

My guess is that you make a mistake while processing the result NodeList. Try the following approach:

NodeList results = (NodeList) xpath.evaluate(..);
for (int i = 0; i < nodelist.getLength(); i++) {
    Node node = (Node) nodelist.item(i);
    ...
}

我的猜测是,在处理结果列表你犯了一个错误。尝试以下方法:

NodeList results = (NodeList) xpath.evaluate(..);
for (int i = 0; i < nodelist.getLength(); i++) {
    Node node = (Node) nodelist.item(i);
    ...
}
java  xml  dom  xpath