[jdom-interest] JTree

Ed Maher ed.maher at euromainbt.co.uk
Wed Jan 28 06:12:42 PST 2004


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.


  ----- 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 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/72dea456/attachment.htm

More information about the jdom-interest mailing list