This feels like such a noob question.
I’m looking at a pile of Java code that manipulates an XML DOM. (The classes are the stock org.w3c.dom.Document and javax.xml.xpath.XPath and such that ship with JDK 7.) It has a ton of places that look like this:
String expr = "/fixed/path/through/the/hierarchy";
// actual code reuses factory instances, etc
XPath xpath = XPathFactory.newInstance().newXPath();
Node topNode = someDocumentInstance.getFirstChild();
Node node = (Node) xpath.evaluate (expr, topNode, XPathConstants.NODE);
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equalsIgnoreCase("somePrefix:someTag")) {
// "return child;" or otherwise break out of the loop
}
}
And it all works. But that loop seems a tedious effort; if we’re already using XPath to get a node, why then iterate over that node’s children looking for a known tag?
So I tried to rewrite a section to fetch the child node directly. But querying using
String expr = "/fixed/path/through/the/hierarchy/somePrefix:someTag";
never matches anything. I’ve tried variations like requesting XPathConstants.NODESET or .STRING, but still no results. (There should only ever be one of these nodes anyhow.)
I feel like I’m missing something supremely obvious here, but I can’t figure out why the full query fails, when the query-for-parent plus a manual loop through the children works. Is XPath testing some quality of a node beyond getNodeName() when I use a query like that?
The only theory I’ve come up with is that it has something to do with XML namespaces, which aren’t used in this project. (There’s actually a call to .setNamespaceAware(false) on the DocumentBuilderFactory instance with a comment saying “leave this off or everything everywhere breaks”.)
If you’re parsing without namespaces, then you should leave
somePrefixout of your expression:The reason for this is that XPath performs matches on namespace and local name, not qualified name (which is what
getNodeName()returns). If you put a prefix in your XPath expression, the XPath interpreter will use that to retrieve the namespace from its namespace mapping. Since you haven’t given it any mappings, that will fail.Also, you probably want to use
NODESETif you’re going to iterate over the child nodes.