<br><font size=2 face="sans-serif">I had the following from one of my extended element (Elementx) classes, you can replace this with the root note for the tree</font>
<br>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>/**</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>*</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>Returns</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>a</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>DefaultMutableTreeNode</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>for</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>use</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>in</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>a</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>JTree</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>which</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>*</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>represents</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>the</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>element</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>*</tt></font><font size=2><tt> </tt></font><font size=2 color=#71b2cf><tt><b>@return</b></tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>DefaultMutableTreeNode</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>*/</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#820040><tt>public</tt></font><font size=2><tt> DefaultMutableTreeNode getTreeNode() {</tt></font>
<br><font size=2><tt> DefaultMutableTreeNode treeNode = </tt></font><font size=2 color=#820040><tt>new</tt></font><font size=2><tt> DefaultMutableTreeNode(</tt></font><font size=2 color=#820040><tt>this</tt></font><font size=2><tt>);</tt></font>
<br><font size=2><tt> addChildren(treeNode, </tt></font><font size=2 color=#820040><tt>this</tt></font><font size=2><tt>);</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#820040><tt>return</tt></font><font size=2><tt> treeNode;</tt></font>
<br><font size=2><tt> }</tt></font>
<br>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>/**</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>*</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>Internal</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>function</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>to</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>support</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>get</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>tree</tt></font><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>node</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#4040c2><tt>*/</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#820040><tt>private</tt></font><font size=2><tt> </tt></font><font size=2 color=#820040><tt>static</tt></font><font size=2><tt> </tt></font><font size=2 color=#820040><tt>void</tt></font><font size=2><tt> addChildren(DefaultMutableTreeNode tn,Elementx element) {</tt></font>
<br><font size=2><tt> DefaultMutableTreeNode node;</tt></font>
<br><font size=2><tt> List children = element.getChildren();</tt></font>
<br><font size=2><tt> Iterator it = children.iterator();</tt></font>
<br><font size=2><tt> </tt></font><font size=2 color=#820040><tt>while</tt></font><font size=2><tt> (it.hasNext()) {</tt></font>
<br><font size=2><tt> Elementx child = (Elementx) it.next();</tt></font>
<br><font size=2><tt> node = </tt></font><font size=2 color=#820040><tt>new</tt></font><font size=2><tt> DefaultMutableTreeNode(child);</tt></font>
<br><font size=2><tt> tn.add(node);</tt></font>
<br><font size=2><tt> addChildren(node, child);</tt></font>
<br><font size=2><tt> };</tt></font>
<br><font size=2><tt> }</tt></font><font size=2 face="sans-serif"><br>
<br>
/Phill<br>
IS Dept, Software Engineer.<br>
phill_perryman@mitel.com<br>
http://www.mitel.com<br>
Tel: +44 1291 436023</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1 face="sans-serif"><b>"J. Albers" <jalbers@cs.uu.nl></b></font>
<br><font size=1 face="sans-serif">Sent by: jdom-interest-admin@jdom.org</font>
<p><font size=1 face="sans-serif">23/01/2004 14:14</font>
<br>
<td><font size=1 face="Arial"> </font>
<br><font size=1 face="sans-serif"> To: <jdom-interest@jdom.org></font>
<br><font size=1 face="sans-serif"> cc: </font>
<br><font size=1 face="sans-serif"> Subject: [jdom-interest] JTree</font></table>
<br>
<br>
<br><font size=2 face="Courier New">Hey,<br>
<br>
I want to list an XML file into a JTree, so that it has the same hierarchy<br>
as the original document. The code now does<br>
only list all elements in one row, but no nesting. Does anyone know how i<br>
can get the elements to be outputted with the same<br>
nesting as in the XML file?<br>
<br>
Grtz, Joachim.<br>
<br>
<br>
private static void listElements(Element e)<br>
{<br>
int depth = 0;<br>
DefaultMutableTreeNode newelements = null;<br>
DefaultMutableTreeNode newchildren = null;<br>
System.out.println("*Element, name:" + e.getName() +<br>
", text:" + e.getText()) ;<br>
newelements = new DefaultMutableTreeNode(e.getName());<br>
Main.elements.add(newelements);<br>
if(e.getText() != "")<br>
{<br>
newchildren = new DefaultMutableTreeNode(e.getText());<br>
Main.elements.add(newchildren);<br>
}<br>
<br>
<br>
//List all attributes<br>
List as = e.getAttributes();<br>
for (Iterator i = as.iterator();i.hasNext();)<br>
{<br>
depth++;<br>
Attribute a = (Attribute)i.next();<br>
System.out.println("*Attribute, name:" + a.getName() +<br>
", value:" + a.getValue()) ;<br>
}<br>
<br>
//List all children<br>
List c = e.getChildren();<br>
<br>
for (Iterator i = c.iterator();i.hasNext();)<br>
{<br>
depth++;<br>
System.out.println("depth = "+depth);<br>
Element n = (Element)i.next();<br>
listElements(n);<br>
}<br>
}<br>
<br>
_______________________________________________<br>
To control your jdom-interest membership:<br>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com<br>
</font>
<br>
<br>