title: Iterate to the end of a NodeList in Java
body: <p>Parsing XML contains incrementing namespace numbers and multiple namespaces. Unable to parse the  last node.  How do you iterate to the last node ?
  Found this class in
  <a href="https://stackoverflow.com/questions/19589231/can-i-iterate-through-a-nodelist-using-for-each-in-java">Can I iterate through a NodeList using for-each in Java?</a>.  It doesn't get the last node when parsing, below is just a sample of the xml.</p>
  <pre><code>    ...
      for (Node node : XMLHelper.getChildNodes(node)){
      ...
      }
  
      public class XMLHelper {
          private XMLHelper() { }
      
          public static List&lt;Node&gt; getChildNodes(NodeList l) {
              List&lt;Node&gt; children = Collections.&lt;Node&gt;emptyList();
              if (l != null &amp;&amp; l.getLength() &gt; 0) {
                  if (l.item(0) != null &amp;&amp; l.item(0).hasChildNodes()) {
                      children = new NodeListWrapper(l.item(0).getChildNodes());
                  }
              }
              return children;
          }
      
          public static List&lt;Node&gt; getChildNodes(Node n) {
              List&lt;Node&gt; children = Collections.&lt;Node&gt;emptyList();
              if (n != null &amp;&amp; n.hasChildNodes()) {
                  NodeList l = n.getChildNodes();
                  if (l != null &amp;&amp; l.getLength() &gt; 0) {
                      children = new NodeListWrapper(l);
                  }
              }
              return children;
          }
      
    
  
    private static final class NodeListWrapper extends AbstractList&lt;Node&gt; implements RandomAccess {
          private final NodeList list;
          NodeListWrapper(NodeList l) {
              list = l;
          }
          public Node get(int index) {
              return list.item(index);
          }
          public int size() {
              return list.getLength();
          }
      }
      
           &lt;TESTXmlResponse xmlns=&quot;TEST/TESTXmlResponse&quot;&gt;
                &lt;firstRequest&gt;
                 &lt;ns1:xmlRquest xmls:ns1=&quot;TEST/XMLRequest&quot;&gt;
                  &lt;ns2:username xmls:ns2=&quot;TEST/XMLUserNameRequest&quot;&gt;
                    &lt;ns3:value xmls:ns3=TEST/XMLValueRequest&quot;&gt;test&lt;/ns3:value&gt;
                  &lt;/ns2:username&gt;
                 &lt;/ns1:xmlRquest&gt;
                &lt;/firstRequest&gt;
                &lt;data&gt;
                  &lt;ns4:name xmls:ns4=&quot;TEST/XMLConstants&quot;&gt;name1&lt;/ns4:name&gt;
                  &lt;ns5:value xmls:ns5=TEST/XMLConstants&quot;&gt;data1&lt;/ns5:value&gt;
                &lt;/data&gt;  
                &lt;data&gt;
                  &lt;ns6:name xmls:ns6=&quot;TEST/XMLConstants&quot;&gt;name2&lt;/ns6:name&gt;
                  &lt;ns7:value xmls:ns7=TEST/XMLConstants&quot;&gt;data2&lt;/ns7:value&gt;
                &lt;/data&gt; 
                &lt;data&gt;
                  &lt;ns8:name xmls:ns8=&quot;TEST/XMLConstants&quot;&gt;name3&lt;/ns8:name&gt;
                  &lt;ns9:value xmls:ns9=TEST/XMLConstants&quot;&gt;data3/ns9:value&gt;
                &lt;/data&gt; 
              &lt;/TESTXmlResponse&gt;
  </code></pre>
  <p>The last node is never parsed no errors the last entry in the parsed file is name 2 data 2  contains</p>