<br><font size=2><tt>The bit of code in the DocumentViewer program below sets the factory for the builder which means all returned Elements are of type Elementx</tt></font>
<br>
<br><font size=2><tt>When I run the DocumentViewer program I do not get any cast errors when opening a file.</tt></font>
<br>
<br><font size=2 color=#3f8080><tt>// create a factory that will return the extended classes</tt></font>
<br><font size=2><tt>JDOMFactoryx factory = </tt></font><font size=2 color=#820040><tt>new</tt></font><font size=2><tt> JDOMFactoryx();</tt></font>
<br>
<br><font size=2 color=#3f8080><tt>// now create a builder and assign the factory to it</tt></font>
<br><font size=2><tt>esb = </tt></font><font size=2 color=#820040><tt>new</tt></font><font size=2><tt> SAXBuilderx();</tt></font>
<br><font size=2><tt>esb.setFactory(factory);</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>"Ed Maher" <ed.maher@euromainbt.co.uk></b></font>
<p><font size=1 face="sans-serif">28/01/2004 14:12</font>
<br>
<td><font size=1 face="Arial"> </font>
<br><font size=1 face="sans-serif"> To: "J. Albers" <jalbers@twc.sshunet.nl>, <Phill_Perryman@Mitel.COM></font>
<br><font size=1 face="sans-serif"> cc: <jdom-interest@jdom.org></font>
<br><font size=1 face="sans-serif"> Subject: Re: [jdom-interest] JTree</font></table>
<br>
<br>
<br><font size=2 face="Arial">Hi,</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="Arial">I also have an interest in xml->JTree linking, so I tried the sample code, but unfortunately, I am getting a class cast</font>
<br><font size=2 face="Arial">exception in SAXHandlerx::endElement() on the line...</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="Arial">Elementx e = (Elementx) currentElement;</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="Arial">but I only get this when loading a document from file.</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="Arial">Surely this could only work if we had actually created an Elementx to populate 'currentElement', I don't see where that is done.</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="Arial">I am particularly confused, as the hard-coded test-data works, file reading does not.</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="Arial">Ed.</font>
<br><font size=3 face="Times New Roman"> </font>
<br><font size=3 face="Times New Roman">----- Original Message ----- </font>
<br><font size=3 face="Times New Roman"><b>From:</b> </font><a href=mailto:Phill_Perryman@Mitel.COM><font size=3 color=blue face="Times New Roman"><u>Phill_Perryman@Mitel.COM</u></font></a><font size=3 face="Times New Roman"> </font>
<br><font size=3 face="Times New Roman"><b>To:</b> </font><a href=mailto:jalbers@twc.sshunet.nl><font size=3 color=blue face="Times New Roman"><u>J. Albers</u></font></a><font size=3 face="Times New Roman"> </font>
<br><font size=3 face="Times New Roman"><b>Cc:</b> </font><a href="mailto:jdom-interest@jdom.org"><font size=3 color=blue face="Times New Roman"><u>jdom-interest@jdom.org</u></font></a><font size=3 face="Times New Roman"> </font>
<br><font size=3 face="Times New Roman"><b>Sent:</b> Tuesday, January 27, 2004 1:06 PM</font>
<br><font size=3 face="Times New Roman"><b>Subject:</b> Re: [jdom-interest] JTree</font>
<br>
<br><font size=2 face="Arial"><br>
>>This works great. Though i tried to figure out what is happening but can't quite get it. Could u maybe explain what happens here(or point me to a site which >>explains it), how is the tree like structure made? And why doesn't it work in my piece of code? Als i wonder if it would be easy to also list the attributes and >>labels?</font><font size=3 face="Times New Roman"> <br>
</font><font size=2 face="sans-serif"><br>
I don't know of any site which explains it. The underlying principle is "Recursion".</font><font size=3 face="Times New Roman"> <br>
</font><font size=2 face="sans-serif"><br>
You start with the root node and then call a method to add the children, the method that adds a child calls a method (actually itself) to add its own children (which calls itself again to add its children, recursively) before moving onto the next element. Trying to work out what level you are on and remember where you are in the tree hierarchy is a nightmare to do when trying to represent it at a flat level. Because the variables in the method call are local to it you can nest the calls without having to keep track of where you are as you get one add children method call per nested element.</font><font size=3 face="Times New Roman"> <br>
</font><font size=2 face="sans-serif"><br>
Don't know if I can explain it much better.</font><font size=3 face="Times New Roman"> <br>
</font><font size=2 face="sans-serif"><br>
for a document like</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
<root></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
<a></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
<x></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
<z/></font><font size=3 face="Times New Roman"> </font>
<br><font size=2 face="sans-serif"> </x></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
<y/></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
</a></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
<b/></font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
</root></font><font size=3 face="Times New Roman"> <br>
</font><font size=2 face="sans-serif"><br>
root calls addChildren</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
add Children adds a to root and then calls add children (to a)</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
add children adds x to a then calls add children (to x)</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
add children adds z to x</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
exit method</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
add children adds y to a</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
exit method</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
add children adds b to root</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
exit method</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
end calling sequence</font><font size=3 face="Times New Roman"> <br>
</font><font size=2 face="sans-serif"><br>
The Java I base my development on is attached if it is any use.</font><font size=3 face="Times New Roman"> </font><font size=2 face="sans-serif"><br>
The tree note uses the user objects toString method to display the tree node. That is why I use an extended Element (Elementx) class so that I can override the toString method in Element to provide the representation in the tree that I want. There is a simple document viewer that shows the JTree formatting</font><font size=3 face="Times New Roman"> <br>
<br>
</font>
<br>
<br>