gyorgyabraham
gyorgyabraham

Reputation: 2608

Java XPath returns single result instead of NodeSet

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.

Upvotes: 1

Views: 746

Answers (1)

Kristof Jozsa
Kristof Jozsa

Reputation: 7522

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);
    ...
}

Upvotes: 2

Related Questions