[jdom-interest] Last call: getChild/getChildren versus getChildElement/getChildElements

Elliotte Rusty Harold elharo at metalab.unc.edu
Fri Feb 6 17:32:07 PST 2004

At 6:43 PM -0600 2/6/04, Bradley S. Huffman wrote:

>Really? Using a little XOM code as an example, I find
>     for (int i = 0; i < element.getChildCount(); i++) {
>         Node child = element.getChild(i);
>         if (child instanceof Element) {
>             processElement(children.get(i));
>         }
>     }
>just as easy as
>     Elements children = element.getChildElements();
>     for (int i = 0; i < children.size(); i++) {
>       processElement(children.get(i));
>     }

If there are a lot of them, the latter is going to be much faster in 
XOM. instanceof is a dog. However, in many situations, it won't be 
slow enough to matter so do what you prefer until profiling proves 

>And is getChildElements("bar") in JDOM and XOM really more convenient
>that dom4j Node.selectNodes("child::bar")? It isn't as powerfull. Is it
>faster? Maybe/maybe not. Will that still be true next year? Agian maybe/maybe

XPath is nicer, and XOM will eventually support it. The problem is it 
isn't sop obvious how to mix it with Java. The simple cases like 
selectNodes("child::bar") work easily; but things get real tricky 
when you consider:

* Elements in namespaces
* XPath variables
* XPath expressions that return something other than a node-set

It's very hard to get the design of this right (as we've seen on this 
list in the last day or two) and I'm not sure dom4j has done it.


   Elliotte Rusty Harold
   elharo at metalab.unc.edu
   Effective XML (Addison-Wesley, 2003)

More information about the jdom-interest mailing list