I have XML that contains a list of customer nodes; I have successfully executed a statement using dom4j that produces a list of these nodes. I am now attempting to iterate through the list to build a second list containing objects represented by these nodes.
I have the following method:
public String getXPathValue(Node node, int ... indices)
{
System.out.println(node.asXML()); // debug
XPath xPath = getXPath(indices);
System.out.println(xPath.getText()); // debug
Node tokenNode = xPath.selectSingleNode(node);
System.out.println(tokenNode == null ? "null" : "not null"); // debug
xPath = new DefaultXPath("./AccountNumber"); // debug
tokenNode = xPath.selectSingleNode(node); // debug
System.out.println(tokenNode == null ? "null" : "not null"); // debug
String result = (tokenNode == null) ? "" : tokenNode.getText();
return result;
}
I've got a useless repetition in here, added while debugging. The idea is that xPath
will select one value from this node, and the tokenNode.getText()
call will obtain the text for that element of this node and return it.
The node.asXML()
call produces the following:
<Item xmlns="http://webservices.xyz.com/Customer/1.0">
<AccountNumber>123</AccountNumber>
<CustomerID>
<AuthenticatedKey>123</AuthenticatedKey>
<ID>123</ID>
</CustomerID>
<CustomerName>TEST customer</CustomerName>
</Item>
The xPath.getText()
call produces this:
/cust:Item/cust:AccountNumber
where 'cust' has been added to the namespace for the XPath map as "http://webservices.xyz.com/Customer/1.0".
But tokenNode ends up as null. I tried an XPath of "/Item/AccountNumber" (i.e., without namespace designations"), tokenNode is still null. I tried "./AccountNumber" and "./cust:AccountNumber", also unsuccessful.
Now, the node was obtained from a larger document, but I've been assuming that, since my node here starts with <Item>
, that I only had to put in XPath info for this node.
Can someone please explain what I'm doing wrong here?