[jdom-interest] Searching a JDOM document

Hushagen, Michael E. MEHushagen at fedins.com
Tue Aug 27 10:11:04 PDT 2002

Here's a JDOM question for all the gurus out there:

I want to search a JDOM document and return the element that matches the
name I pass in the parameters.  Here's what I have (it's recursive):

public Element findElement(Element current, String elementName)
	if (current.getName() == elementName)
		return current;
	List children = current.getChildren();
	Iterator iterator = children.iterator();
	while (iterator.hasNext())
		Element child = (Element) iterator.next();
		Element tempElement = findElement(child, elementName);
		if (tempElement != null)
			return tempElement;
             // Didn't find the element anywhere.
	return null;

I call this method with the JDOM root element.  It works fine, but I was
wondering if there is a more efficient or official way of doing this.

 -Mike H.
This information is intended only for the use of the addressee(s) and may contain privileged, confidential or proprietary information.  If you are not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution, displaying, copying, or use of this information is strictly prohibited.  If you have received this communication in error, please notify us immediately at Administrator at fedins.com or by telephone at (800) 533-0472, and return the information to the sender with all copies deleted and destroyed.  Thank you.

More information about the jdom-interest mailing list