title: Looping XML with the lowest complexity
body: <p>I am using Java DOM parser to parse XML files. Performance is important to me and I need to write the most optimized code. I noticed that XMLs to be processed have a lot of nested tags (It can reach 5 levels in depth) and I have to retrieve information of all levels.</p>
  
  <p>The dummy solution I implemented is to have nested loops each loop retrieves the node's children and passes it to the next loop.</p>
  
  <p>This is a very bad practice on the level of performance and complexity as code complexity reaches O(n^5). Please, find how it can go on the code level below.</p>
  
  <p>I believe divide and conquer algorithms might work in this case.</p>
  
  <p>Do any of you have any suggestions to have a more optimized code to have better performance?</p>
  
  <blockquote>
    <p>for (int temp = 0; temp &lt; contractDetails.getLength(); temp++){
              Node detail = contractDetails.item(temp);
              System.out.println(detail.getNodeName());
              NodeList detail2 = detail.getChildNodes();</p>
  
  <pre><code>      for (int temp2 = 0; temp &lt; detail2.getLength(); temp2++){
        .........
            for (int temp3 = 0; temp &lt; detail3.getLength(); temp3++){
                ...............
            }
  
        }
     }
  </code></pre>
  </blockquote>