[jdom-interest] JTree

J. Albers jalbers at twc.sshunet.nl
Tue Jan 27 03:08:30 PST 2004

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?

Kind regards, 
    Joachim Albers.
  ----- Original Message ----- 
  From: Phill_Perryman at Mitel.COM 
  To: J. Albers 
  Cc: jdom-interest at jdom.org ; jdom-interest-admin at jdom.org 
  Sent: Friday, January 23, 2004 3:31 PM
  Subject: Re: [jdom-interest] JTree

  I had the following from one of my extended element (Elementx) classes, you can replace this with the root note for the tree 

           * Returns a DefaultMutableTreeNode for use in a JTree which 
           * represents the element 
           * @return DefaultMutableTreeNode 
          public DefaultMutableTreeNode getTreeNode() { 
                  DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(this); 
                  addChildren(treeNode, this); 
                  return treeNode; 

           * Internal function to support get tree node 
          private static void addChildren(DefaultMutableTreeNode tn,Elementx element) { 
                  DefaultMutableTreeNode node; 
                  List children = element.getChildren(); 
                  Iterator it = children.iterator(); 
                  while (it.hasNext()) { 
                          Elementx child = (Elementx) it.next(); 
                          node = new DefaultMutableTreeNode(child); 
                          addChildren(node, child); 

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

       "J. Albers" <jalbers at cs.uu.nl> 
        Sent by: jdom-interest-admin at jdom.org 
        23/01/2004 14:14 

                To:        <jdom-interest at jdom.org> 
                Subject:        [jdom-interest] JTree 


  I want to list an XML file into  a JTree, so that it has the same hierarchy
  as the original document. The code now does
  only list all elements in one row, but no nesting. Does anyone know how i
  can get the elements to be outputted with the same
  nesting as in the XML file?

  Grtz, Joachim.

  private static void listElements(Element e)
   int depth = 0;
   DefaultMutableTreeNode newelements = null;
   DefaultMutableTreeNode newchildren = null;
   System.out.println("*Element, name:" + e.getName() +
    ", text:" + e.getText()) ;
   newelements = new DefaultMutableTreeNode(e.getName());
   if(e.getText() != "")
    newchildren = new DefaultMutableTreeNode(e.getText());

   //List all attributes
   List as = e.getAttributes();
   for (Iterator i = as.iterator();i.hasNext();)
    Attribute a = (Attribute)i.next();
    System.out.println("*Attribute, name:" + a.getName() +
     ", value:" + a.getValue()) ;

   //List all children
   List c = e.getChildren();

   for (Iterator i = c.iterator();i.hasNext();)
    System.out.println("depth = "+depth);
    Element n = (Element)i.next();

  To control your jdom-interest membership:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040127/5cc55e00/attachment.htm

More information about the jdom-interest mailing list