Daniel Lemire's blog

, 8 min read

XPath support in Java 1.5

10 thoughts on “XPath support in Java 1.5”

  1. didier says:

    Hey Daniel, check your rss feed (http://www.daniel-lemire.com/blog/feed/rss2/). It seems to mal-formed and the cultprit seem to be the code section in sthis post.

  2. Thanks. WordPress is still a bit weak as far as enforcing well formedness of the XHTML output.

  3. It probably would not be that hard to write a iterator for the NodeList object or even extend NodeList to implement Iterator. If you did that, foreach should work just fine.

  4. Daniel Lemire says:

    The problem is that the Sun engineers should have made the foreach construct work with their API.

    Basically, I think that Sun’s leadership with regard to Java is damaging, we don’t have a the benefit of a community-based approach (Sun keeps on reinventing what other people have done) and we have the drawbacks of a community-based approach (an heterogeneous API).

  5. John, did I promote .NET? No I did not. Life is not a choice between Java and C#. There are many other languages out there… Python, PHP, Lisp, Scheme, Haskell, Prolog, BASIC, Delphi…

    Some, like Python, implement iterators in an elegant and consistent fashion.

    In fact, using Jython, under a JVM, you get iterators that work well. So the problem is not with the Java architecture, but rather with the fact that the Java language itself is a bit of a hack and it is certainly not as good as it could have been.

    I don’t use .NET. I don’t use C#. I don’t care about these technologies and I’m never comparing Java to C# or VB.net.

  6. John Samson says:

    A little missing like forEach is far out-wieghed by the rest of the advantages that java offers. You’re obviously acutely ignorant. Why don’t you go use .Net and see if your code still runs when .Net2 comes out.

  7. I don’t know how invalid my criticism is, but you make a good point “Anonymous”.

  8. Anonymous says:

    NodeList has nothign to do with Sun. Is is a w3c dom API. And of course, the DOM API has to catch up to Java 1.5 to offer an iterable NodeList. So your accusation of Sun and Java is plain invalid.

  9. However, while NodeList doesn’t belong to Sun, several other objects belonging to Sun don’t support foreach. For example, you cannot use foreach with an iterator (why not?).

  10. Sid says:

    FYI … This is from the 1.5 tutorial and explains when the for each construct is used in java:

    Traversing Collections
    There are two ways to traverse collections: (1)with the for-each construct and (2) by using Iterators.
    for-each Construct
    The for-each construct allows you to concisely traverse a collection or array using a for loop — see The for Statement. The following code uses the for-each construct to print out each element of a collection on a separate line.
    for (Object o : collection)
    System.out.println(o);

    Iterators
    An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired. You get an Iterator for a collection by calling its iterator method. The following is the Iterator interface.
    public interface Iterator {
    boolean hasNext();
    E next();
    void remove(); //optional
    }

    The hasNext method returns true if the iteration has more elements, and the next method returns the next element in the iteration. The remove method removes the last element that was returned by next from the underlying Collection. The remove method may be called only once per call to next and throws an exception if this rule is violated.
    Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

    Use Iterator instead of the for-each construct when you need to:

    Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
    Replace elements in a list or array as you traverse it.
    Iterate over multiple collections in parallel.
    The following method shows you how to use an Iterator to filter an arbitrary Collection — that is, traverse the collection removing specific elements.
    static void filter(Collection c) {
    for (Iterator i = c.iterator(); i.hasNext(); )
    if (!cond(i.next()))
    i.remove();
    }

    This simple piece of code is polymorphic, which means that it works for any Collection regardless of implementation. This example demonstrates how easy it is to write a polymorphic algorithm using the Java Collections Framework.