title: Can I iterate through a NodeList using for-each in Java?
body: <p>I want to iterate through a <code>NodeList</code> using a for-each loop in Java. I have it working with a for loop and a do-while loop but not for-each.</p>
  
  <pre><code>NodeList nList = dom.getElementsByTagName("year");
  do {
      Element ele = (Element) nList.item(i);
      list.add(ele.getElementsByTagName("MonthId").item(0).getTextContent());
      i++;
  } while (i &lt; nList.getLength());
  
  NodeList nList = dom.getElementsByTagName("year");
  
  for (int i = 0; i &lt; nList.getLength(); i++) {
      Element ele = (Element) nList.item(i);
      list.add(ele.getElementsByTagName("MonthId").item(0).getTextContent());
  }
  </code></pre>