[jdom-interest] Searching tree for all elements

bob mcwhirter bob at werken.com
Sat Nov 11 07:29:32 PST 2000

> I'm new to JDOM, but I'm excited by what I've seen so far.  My question is
> regarding the ability to load an XML document and perform a query that
> returns all children with a specific name, regardless of their position in
> the structure of the document.
> For example, I'd like to be able to load an XHTML document, and perform a
> query to retrieve all INPUT elements.
> getChildren() only returns the direct children of an Element.  A brute force
> way to do this would be to write a function that recurses over all the
> elements, and returns the elements that match.  However, I was wondering if
> there was a more optimal way to do this.

Sounds like you might be interested in XPath.  XPath is a W3C Recommendation
at this point, used in XSLT, and is a form of addressing parts of an XML

In your case, the XPath of "//INPUT" would find any/all <INPUT> elements
in your document.

A handy-dandy (but not-quite-done) implementation of an XPath engine
is available at:


It does end up still doing a roughly brute-force search, but that's
handled by the XPath engine, and results in only about 2 lines of
code in your application, instead of lots of loops and iterations 
and comparisons.

Please, check it out, and if you have any problems/questions, don't
hesitate to contact me, as I'm it's author.


More information about the jdom-interest mailing list