1 Reply Latest reply: Apr 29, 2007 10:09 PM by DrClap RSS

    Print XML from tree

    807606
      Hey, I'm trying to print tree nodes into XML. The code below prints out the tags, but I have no way of getting the end tags identified and printed. Any help will be much appreciated.
      public static void printDescendents(TreeNode node){
      System.out.prinln(node);   
      Enumeration children = root.children();
         if (children != null){
            while children.hasMoreElements()){
               printDescendents((TreeNode) children.nextElement());
            }
         }
      }
        • 1. Re: Print XML from tree
          DrClap
          I have no idea whether printing the node prints a start tag or an end tag or what. But here's what I would do:
          public static void printDescendants(TreeNode node){
            // print start tag
            Enumeration children = node.children();
            if (children != null){
              while children.hasMoreElements()){
                printDescendants((TreeNode) children.nextElement());
              }
            }
            // print end tag
          }
          I also corrected the spelling of your method name and changed the Enumeration so it gets the children of the node the method is dealing with.

          Looking at the result, I wouldn't call the method printDescendants if it's also going to print data from the node itself. Maybe printNode would be a better name.