title: slow construction of tree structure from XML
body: <p>I'm parsing an XML document into my own structure but building it is very slow for large inputs is there a better way to do it?</p>
  
  <pre><code>public static DomTree&lt;String&gt; createTreeInstance(String path) 
    throws ParserConfigurationException, SAXException, IOException {
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
      File f = new File(path);
      Document doc = db.parse(f);       
      Node node = doc.getDocumentElement(); 
      DomTree&lt;String&gt; tree = new DomTree&lt;String&gt;(node);
      return tree;
  }
  </code></pre>
  
  <p>Here is my DomTree constructor:</p>
  
  <pre><code>    /**
       * Recursively builds a tree structure from a DOM object.
       * @param root
       */
      public DomTree(Node root){      
          node = root;        
          NodeList children = root.getChildNodes();
          DomTree&lt;String&gt; child = null;
          for(int i = 0; i &lt; children.getLength(); i++){  
              child = new DomTree&lt;String&gt;(children.item(i));
              if (children.item(i).getNodeType() != Node.TEXT_NODE){
                  super.children.add(child);
              }
          }
      }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>I have benchmarked the createTreeInstance() method using a 100MB XML file:</p>
  
  <ul>
  <li>Creating docBuilderFactory...     Done [3ms] </li>
  <li>Creating docBuilder...    Done [21ms]</li>
  <li>parsing file...   Done [5646ms]</li>
  <li>getDocumentElement...    Done [1ms]</li>
  <li>creating DomTree...  Done [17076ms]</li>
  </ul>
  
  <p>UPDATE:</p>
  
  <p>As John Doe suggests below it may be more appropriate to use SAX - I have never used SAX before, so is there a good way to convert what I have to using SAX?</p>