[jdom-interest] JTree

Phill_Perryman at Mitel.COM Phill_Perryman at Mitel.COM
Fri Jan 23 06:31:54 PST 2004


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);
                        tn.add(node);
                        addChildren(node, child);
                };
        }

/Phill
IS Dept, Software Engineer.
phill_perryman at mitel.com
http://www.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>
        cc: 
        Subject:        [jdom-interest] JTree


Hey,

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());
  Main.elements.add(newelements);
  if(e.getText() != "")
  {
   newchildren = new DefaultMutableTreeNode(e.getText());
   Main.elements.add(newchildren);
  }


  //List all attributes
  List as = e.getAttributes();
  for (Iterator i = as.iterator();i.hasNext();)
  {
   depth++;
   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();)
  {
   depth++;
   System.out.println("depth = "+depth);
   Element n = (Element)i.next();
   listElements(n);
  }
 }

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040123/cbd96c99/attachment.htm


More information about the jdom-interest mailing list