Wednesday, September 27, 2006

The hell of NodeList

Probably a more better title could be “The hell of NodeList and a bad boy that doesn’t read the documentation” (with Java 1.5).
Yes, this little thing (reading documentation) is a very good thing if you want to have headaches working with NodeLists.
The summary of this painful lesson is:

  • If you work with an XML document you must remember that:

    The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live. (

    that is, if you change or remove nodes, your changing and removing directly to the document.

  • But if you work with XPath to evaluate expression and get node references, you must remeber it uses Xalan and the NodeSet class implements the NodeList interface and:

    Note that we directly implement the DOM’s NodeIterator interface. We do not emulate all the behavior of the standard NodeIterator. In particular, we do not guarantee to present a “live view” of the document


This means not is the same a NodeList returnes by a getChildNodes() than a NodeList returned by an xpath evaluate() method.