title: Need to get the parent id but I'm getting level of the xml tag in Java
body: <p>Below is my XML</p>
  
  <pre><code>&lt;jndi&gt; 
      &lt;ejbmodule&gt; 
          &lt;context&gt;
              &lt;leaf&gt; 
                  &lt;name&gt;XAConnectionFactory&lt;/name&gt; 
                  &lt;type&gt;type1&lt;/type&gt; 
                  &lt;year&gt;2002&lt;/year&gt;
                  &lt;attribute name="class"&gt;org.jboss.mq.SpyXAConnectionFactory&lt;/attribute&gt; 
                  &lt;test1&gt;
                      &lt;name&gt;mytest&lt;/name&gt;
                  &lt;/test1&gt;
              &lt;/leaf&gt; 
              &lt;name&gt;java&lt;/name&gt; 
              &lt;type&gt;type2&lt;/type&gt; 
              &lt;year&gt;2003&lt;/year&gt;           
              &lt;leaf&gt; 
                  &lt;name&gt;DefaultDS&lt;/name&gt; 
                  &lt;type&gt;type3&lt;/type&gt; 
                  &lt;year&gt;2004&lt;/year&gt;
                  &lt;attribute name="class"&gt;javax.sql.DataSource&lt;/attribute&gt; 
              &lt;/leaf&gt; 
          &lt;/context&gt;
      &lt;/ejbmodule&gt;    
  &lt;/jndi&gt;
  </code></pre>
  
  <p>Below is my java code where I'm traversing the XML and converting it to a DTree javascript code to display the XML as a tree structure on the html page</p>
  
  <pre><code>import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import org.w3c.dom.Document;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  public class RecursiveDOM {
  
      static int globalCounter = 0;
  
      public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
          new RecursiveDOM("C:\\Users\\Rajani\\Downloads\\SampleXMLData\\dTree_1\\new1.xml");
      }
  
      public RecursiveDOM(final String file) throws SAXException, IOException, ParserConfigurationException {
          final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
          final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
          InputSource is = new InputSource(new FileReader(new File(file)));
          final Document doc = docBuilder.parse(is);
          StringBuffer html = new StringBuffer();
          int parentId = -1;
  
          html.append("&lt;div class=\"dtree\"&gt;&lt;script type=\"text/javascript\" src=\"dtree.js\"&gt;&lt;/script&gt;\n");
          html.append(createTreeCommandLinks("decisionReportTree"));
          // create tree and add root node
          html.append("&lt;script type=\"text/javascript\"&gt;\n&lt;!--\n");
          html.append("decisionReportTree = new dTree('decisionReportTree');\n");
          html.append("decisionReportTree.add(" + globalCounter++ + ",-1,'List Persons');\n");
  
          traverse(doc, parentId, html);
  
          html.append("document.write(decisionReportTree);");
          html.append("\n//--&gt;\n&lt;/script&gt;");
          System.out.println(html);
      }
  
      private static String add(String tree, int global, int parent, String name) {
          return tree + ".add(" + global + ", " + parent + ", '" + name + "');\n";
      }
  
      public static void traverse(Node node, int parentId, StringBuffer html) {
          handle_node(node, parentId, html);
          if (node.hasChildNodes()) {
              NodeList children = node.getChildNodes();
              for (int i = 0; i &lt; children.getLength(); i++) {
                  Node kid = children.item(i);
                  if (kid.getNodeType() == Node.ELEMENT_NODE) {
                      traverse(kid, parentId + 1, html);
                  }
              }
          }
      }
  
      public static void handle_node(Node node, int parentId, StringBuffer html) {
          if (node.getNodeType() == Node.ELEMENT_NODE) {
              String value = getNodeValue(node);
  
              if(null != value &amp;&amp; value.trim().length() &gt; 0) {
                  html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName() + ":::" + value));
              } else {
                  html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName()));
              }
          }
      }
  
      private static String createTreeCommandLinks(String treeName) {
          return "&lt;p&gt;&lt;a href=\"javascript: " + treeName + ".openAll();\"&gt;Expands all&lt;/a&gt; | &lt;a href=\"javascript: " + treeName + ".closeAll();\"&gt;Collapse all&lt;/a&gt;&lt;/p&gt;";
      }
  
      protected static String getNodeValue(Node node) {
          NodeList childNodes = node.getChildNodes();
          for (int x = 0; x &lt; childNodes.getLength(); x++) {
              Node data = childNodes.item(x);
              if (data.getNodeType() == Node.TEXT_NODE)
                  return data.getNodeValue();
          }
          return "";
      }
  
      protected String getNodeAttrValue(String attrName, Node node) {
          NamedNodeMap attrs = node.getAttributes();
          for (int y = 0; y &lt; attrs.getLength(); y++) {
              Node attr = attrs.item(y);
              if (attr.getNodeName().equalsIgnoreCase(attrName)) {
                  return attr.getNodeValue();
              }
          }
          return "";
      }
  }
  </code></pre>
  
  <p>The output I'm getting with the above program is</p>
  
  <pre><code>decisionReportTree = new dTree('decisionReportTree');
  decisionReportTree.add(0,-1,'List Persons');
  decisionReportTree.add(1, 0, 'jndi');
  decisionReportTree.add(2, 1, 'ejbmodule');
  decisionReportTree.add(3, 2, 'context');
  decisionReportTree.add(4, 3, 'leaf');
  decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
  decisionReportTree.add(6, 4, 'type:::type1');
  decisionReportTree.add(7, 4, 'year:::2002');
  decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
  decisionReportTree.add(9, 4, 'test1');
  decisionReportTree.add(10, 5, 'name:::mytest');
  decisionReportTree.add(11, 3, 'name:::java');
  decisionReportTree.add(12, 3, 'type:::type2');
  decisionReportTree.add(13, 3, 'year:::2003');
  decisionReportTree.add(14, 3, 'leaf');
  decisionReportTree.add(15, 4, 'name:::DefaultDS');
  decisionReportTree.add(16, 4, 'type:::type3');
  decisionReportTree.add(17, 4, 'year:::2004');
  decisionReportTree.add(18, 4, 'attribute:::javax.sql.DataSource');
  document.write(decisionReportTree);
  </code></pre>
  
  <p>But I need the below output</p>
  
  <pre><code>decisionReportTree = new dTree('decisionReportTree');
  decisionReportTree.add(0,-1,'List Persons');
  decisionReportTree.add(1, 0, 'jndi');
  decisionReportTree.add(2, 1, 'ejbmodule');
  decisionReportTree.add(3, 2, 'context');
  decisionReportTree.add(4, 3, 'leaf');
  decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
  decisionReportTree.add(6, 4, 'type:::type1');
  decisionReportTree.add(7, 4, 'year:::2002');
  decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
  decisionReportTree.add(9, 4, 'test1');
  decisionReportTree.add(10, 9, 'name:::mytest');
  decisionReportTree.add(11, 3, 'name:::java');
  decisionReportTree.add(12, 3, 'type:::type2');
  decisionReportTree.add(13, 3, 'year:::2003');
  decisionReportTree.add(14, 3, 'leaf');
  decisionReportTree.add(15, 14, 'name:::DefaultDS');
  decisionReportTree.add(16, 14, 'type:::type3');
  decisionReportTree.add(17, 14, 'year:::2004');
  decisionReportTree.add(18, 14, 'attribute:::javax.sql.DataSource');
  document.write(decisionReportTree);
  </code></pre>