I\'ve spent the past day attempting to extract a one XML node out of the following document and am unable to grasp the nuances of XML Namespaces to make it work.
The
Have a look at the XPathAPI library. It is a simpler way to use XPath without messing with the low-level Java API, especially when dealing with namespaces.
The code to get the number
attribute would be:
String num = XPathAPI.selectSingleNodeAsString(doc, '//documentnbr/@number');
Namespaces are automatically extracted from the root node (doc
in this case). In case you need to explicitly define additional namespaces you can use this:
Map nsMap = new HashMap();
nsMap.put("xforms", "http://www.w3.org/2003/xforms");
String num =
XPathAPI.selectSingleNodeAsString(doc, '//documentnbr/@number', nsMap);
(Disclaimer: I'm the author of the library.)