<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [jdom-interest] traversing the JDOM tree</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>Fixed a little bug in my previous code, and added tree traversal.</FONT>
<BR><FONT SIZE=2>Hope it helps.</FONT>
</P>
<BR>
<P><FONT SIZE=2>/**</FONT>
<BR><FONT SIZE=2> * Returns a list of all elements that are descendents of the specified element.</FONT>
<BR><FONT SIZE=2> * If no elements are found an empty list is returned.</FONT>
<BR><FONT SIZE=2> */ </FONT>
<BR><FONT SIZE=2>public static List getSubElements(Element element) {</FONT>
<BR><FONT SIZE=2> List subElements = new ArrayList();</FONT>
<BR><FONT SIZE=2> addSubElements(element, null, subElements);</FONT>
<BR><FONT SIZE=2> return subElements;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>/**</FONT>
<BR><FONT SIZE=2> * Returns a list of all elements that are descendents of the specified element,</FONT>
<BR><FONT SIZE=2> * and that has a name equal to the specified name.</FONT>
<BR><FONT SIZE=2> * If no elements are found an empty list is returned.</FONT>
<BR><FONT SIZE=2> */ </FONT>
<BR><FONT SIZE=2>public static List getSubElements(Element element, String name) {</FONT>
<BR><FONT SIZE=2> List subElements = new ArrayList();</FONT>
<BR><FONT SIZE=2> addSubElements(element, name, subElements);</FONT>
<BR><FONT SIZE=2> return subElements;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>private static void addSubElements(Element element, String name, List elements) {</FONT>
<BR><FONT SIZE=2> for (Iterator i = element.getChildren().iterator(); i.hasNext();) {</FONT>
<BR><FONT SIZE=2> Element subElement = (Element)i.next();</FONT>
<BR><FONT SIZE=2> if (name != null && name.equals(subElement.getName())) {</FONT>
<BR><FONT SIZE=2> elements.add(subElement);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> addSubElements(subElement, name, elements);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>public interface Operation {</FONT>
<BR><FONT SIZE=2> void operate(Element element);</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>/**</FONT>
<BR><FONT SIZE=2> * Traverses and applies the specified operation to the tree of elements that</FONT>
<BR><FONT SIZE=2> * are descendents of the specified element. If a name is specified, only</FONT>
<BR><FONT SIZE=2> * elements that match that name are operated upon.</FONT>
<BR><FONT SIZE=2> *</FONT>
<BR><FONT SIZE=2> * @param element The root element of the traversal.</FONT>
<BR><FONT SIZE=2> * @param name If non null, only elements with a name equal to it are operated upon.</FONT>
<BR><FONT SIZE=2> * @param operation The operation to apply.</FONT>
<BR><FONT SIZE=2> */</FONT>
<BR><FONT SIZE=2>public static void traverseElementTree(Element element, String name, Operation operation) {</FONT>
<BR><FONT SIZE=2> if (name != null && name.equals(element.getName())) {</FONT>
<BR><FONT SIZE=2> operation.operate(element);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> List children = (name == null) ? element.getChildren() : element.getChildren(name);</FONT>
<BR><FONT SIZE=2> for (Iterator i = children.iterator(); i.hasNext();) {</FONT>
<BR><FONT SIZE=2> Element subElement = (Element)i.next();</FONT>
<BR><FONT SIZE=2> traverseElementTree(subElement, name, operation);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>///Odd Moller </FONT>
</P>
<BR>
<P><FONT SIZE=2>-----Original Message----- </FONT>
<BR><FONT SIZE=2>From: Vivek Kapadekar [<A HREF="mailto:vkapadekar@bitfone.com">mailto:vkapadekar@bitfone.com</A>] </FONT>
<BR><FONT SIZE=2>Sent: den 15 april 2003 18:59 </FONT>
<BR><FONT SIZE=2>To: jdom-interest@jdom.org </FONT>
<BR><FONT SIZE=2>Subject: [jdom-interest] traversing the JDOM tree </FONT>
</P>
<BR>
<BR>
<P><FONT SIZE=2>hi </FONT>
<BR><FONT SIZE=2>I am using JDOM for the first time. It looks really cool, but one </FONT>
<BR><FONT SIZE=2>functionality that I am looking for which is missing in it. And that is </FONT>
<BR><FONT SIZE=2>for traversing the JDOM tree. </FONT>
<BR><FONT SIZE=2>The getChild and getChildren gets only the elements one level deep. What </FONT>
<BR><FONT SIZE=2>If i want to recursively find and Element or check existence of an </FONT>
<BR><FONT SIZE=2>element, like getElementByName() or findElement(). </FONT>
<BR><FONT SIZE=2>Thanks </FONT>
<BR><FONT SIZE=2>--Vivek </FONT>
</P>
<BR>
<P><FONT SIZE=2>_______________________________________________ </FONT>
<BR><FONT SIZE=2>To control your jdom-interest membership: </FONT>
<BR><FONT SIZE=2><A HREF="http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com" TARGET="_blank">http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com</A> </FONT>
</P>
</BODY>
</HTML>