[jdom-interest] Searching a JDOM document

Xuemin Guan xuemin at appresso.com
Tue Aug 27 18:21:56 PDT 2002


Hi, there. Take a look at the JDOM API for the Element class, you will 
find a few ways to do this better:
such as:

Element getChild(java.lang.String name)
Element getChild(java.lang.String name, Namespace ns)
java.util.List getChildren()
java.util.List getChildren(java.lang.String name)

Alternatively,

you can use XPath with JDOM, it will make your code very neat. For more 
infos on this, take a look at JDOM FAQ (www.jdom.org)

Thanks,

Xuemin


Hushagen, Michael E. wrote:

> 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.
> 
> Thanks!
>  -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:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
> 
> 
> 


-- 
Xuemin Guan
Java & XML Developer
Appresso SpA (www.appresso.com)
Tel: +81-3-4412-7790 (Direct)
      +81-3-4412-7700 (Representative)	
Email: xuemin at appresso.com




More information about the jdom-interest mailing list