[jdom-interest] JTree

Phill_Perryman at Mitel.COM Phill_Perryman at Mitel.COM
Wed Jan 28 07:34:39 PST 2004


The bit of code in the DocumentViewer program below sets the factory for 
the builder which means all returned Elements are of type Elementx

When I run the DocumentViewer program I do not get any cast errors when 
opening a file.

// create a factory that will return the extended classes
JDOMFactoryx factory = new JDOMFactoryx();

// now create a builder and assign the factory to it
esb = new SAXBuilderx();
esb.setFactory(factory);

/Phill
IS Dept, Software Engineer.
phill_perryman at mitel.com
http://www.mitel.com
Tel: +44 1291 436023




"Ed Maher" <ed.maher at euromainbt.co.uk>
28/01/2004 14:12

 
        To:     "J. Albers" <jalbers at twc.sshunet.nl>, <Phill_Perryman at Mitel.COM>
        cc:     <jdom-interest at jdom.org>
        Subject:        Re: [jdom-interest] JTree


Hi,
 
I also have an interest in xml->JTree linking, so I tried the sample code, 
but unfortunately, I am getting a class cast
exception in SAXHandlerx::endElement() on the line...
 
Elementx e = (Elementx) currentElement;
 
but I only get this when loading a document from file.
 
Surely this could only work if we had actually created an Elementx to 
populate 'currentElement', I don't see where that is done.
 
I am particularly confused, as the hard-coded test-data works, file 
reading does not.
 
Ed.
 
----- Original Message ----- 
From: Phill_Perryman at Mitel.COM 
To: J. Albers 
Cc: jdom-interest at jdom.org 
Sent: Tuesday, January 27, 2004 1:06 PM
Subject: Re: [jdom-interest] JTree


>>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? 

I don't know of any site which explains it. The underlying principle is 
"Recursion". 

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. 

Don't know if I can explain it much better. 

for a document like 
<root> 
  <a> 
    <x> 
       <z/> 
     </x> 
    <y/> 
  </a> 
  <b/> 
</root> 

root calls addChildren 
   add Children adds a to root and then calls add children (to a) 
        add children adds x to a then calls add children (to x) 
            add children adds z to x 
            exit method 
        add children adds y to a 
        exit method 
  add children adds b to root 
  exit method 
end calling sequence 

The Java I base my development on is attached if it is any use. 
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 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040128/243d21d4/attachment.htm


More information about the jdom-interest mailing list