0

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?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
arcy
  • 12,845
  • 12
  • 58
  • 103
  • Try applying `//*[local-name() = 'AccountNumber']` to see if you can grab `AccountNumber` independent of its namespace as a test to determine whether namespaces are thwarting your XPath selection. If you can, next look into how you're associating the namespace prefix with the namespace URI and whether it actually affects the node at which you're applying the XPath. I.e. Does the prefix still apply to a detached node? ...to a copy of a node? – kjhughes Mar 09 '16 at 20:14
  • ok, that got me a node with "AccountNumber" as the name and qualified name of its qname. I used "new DefaultXPath("//*[local-name() = 'AccountNumber']")". Whats does that mean? – arcy Mar 09 '16 at 20:56
  • I tried putting the URI into a browser, but don't get anything back -- is that an indication that the namespace won't work? I've always been a little vague about namespaces... – arcy Mar 09 '16 at 21:02
  • Being able to retrieve the node via its local name means that you've probably not properly declared the namespace prefix. Not being able to retrieve anything from a namespace name means nothing, because [namespace URIs do *not* have to be retrievable](http://stackoverflow.com/questions/27613891/must-an-xml-namespace-name-uri-be-retrievable) anyway. – kjhughes Mar 09 '16 at 21:07
  • Great. Now what do I do? – arcy Mar 09 '16 at 21:12

0 Answers0