[jdom-interest] Searching a JDOM document

Frank Sauer Frank.Sauer at trcinc.com
Tue Aug 27 10:16:43 PDT 2002

try Jaxen and an XPath expression like //elementName

-----Original Message-----
From: Hushagen, Michael E. [mailto:MEHushagen at fedins.com]
Sent: Tuesday, August 27, 2002 1:11 PM
To: 'jdom-interest at jdom.org'
Subject: [jdom-interest] Searching a JDOM document

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.

To control your jdom-interest membership:

More information about the jdom-interest mailing list