0

Can someone be kind enough to explain me why
xPath.evaluate("/MEMBER_LIST/MEMBER[1]/ADDRESS", nodeMemberList, XPathConstants.STRING) Returns the value I'm looking for and why xPath.evaluate("/MEMBER_LIST/MEMBER[" + i + "]/ADDRESS", nodeMemberList, XPathConstants.STRING) Returns an empty String? I have to do these in a for loop so here "i" is an int representing the current entry.

1
  • Thank you guys. Thank God it was something as dumb as this. I'll keep my bitter comments for the few doc I found online stating 0 as the starting index with XPath Commented Jul 7, 2010 at 22:20

2 Answers 2

1

Does your for loop start at 1? The expression /MEMBER_LIST/MEMBER[0] isn't a valid XPath expression because XPath indexes start at 1. Also, accessing an index which exceeds the total number of nodes is invalid. For example, executing /MEMBER_LIST/MEMBER[5] when there are only 3 MEMBER elements.

You can also use the XPathConstants.NODESET constant. This will return a list of all elements that match the given expression. You can then iterate over the list.

NodeList nodeList = (NodeList)xPath.evaluate("/MEMBER_LIST/MEMBER/ADDRESS", nodeMemberList, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i){
  Node node = nodeList.item(i);
  String address = node.getTextContent();
}
Sign up to request clarification or add additional context in comments.

Comments

0

If i==0 the answer will be empty as XPath indexes start at 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.