Daniel Lemire's blog

, 1 min read

XPath support in Java 1.5

Things are getting somewhat better in Java land. You can no do some XPath work in Java, see this sample code I wrote this morning (it is not standalone though):


    String xpathexpression = "//xdoc[dtd!='']/fname/text()";
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource indexname_input = new InputSource(indexname);
    NodeList nl = (NodeList) xpath.evaluate(xpathexpression, 
                              indexname_input, XPathConstants.NODESET);
    for (int i = 0; i < nl.getLength(); ++i) {
      System.out.println("loading document " + (i + 1) + " of " + nl.getLength());
      System.out.println("It uses DTD: "+xpath.evaluate("../../dtd",nl.item(i)));
      String xmlfile = nl.item(i).getNodeValue();
      String xmlPath = baseurl + datadir + xmlfile;
    }

However, I was disappointed to see that the new “foreach” construct in Java doesn’t apply to NodeList objects… I’m sorry, but I getting more and more convinced, with every version, that Java is an ugly hack. I mean, you have a collection of nodes, a standard one at that, and you can’t “foreach” it… what gives?

What is the “foreach” construct: Java 1.5 introduces the idea, well known in many languages, of the “foreach” construct. In effect, if you have a set of elements and want to go through them one at a time, using “for(int i=0; i < length; ++i)” is ugly and error-prone. It is much better to do ” for (element in set) “. Java 1.5 now has this as “for (type element : set)”. This being said, I was under the impression that the Java people had been careful to make sure that the “foreach” construct would work with all standard collections of objects… not so, alas.