[jdom-interest] JTree

J. Albers jalbers at cs.uu.nl
Fri Jan 23 06:14:59 PST 2004


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);
  }
 }




More information about the jdom-interest mailing list