title: Java - NodeList can't get Childnode
body: <p>I don't understand how to get child node parsing xml file with java. For example I have this code:</p>
  
  <pre><code>import java.io.File;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class Parser {
  
      public static void main(String args[]) {
  
          try{
              File stocks = new File("Stocks.xml");
              DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.parse(stocks);
              doc.getDocumentElement().normalize();
  
  
  
              NodeList nodes = doc.getElementsByTagName("stock");
              Node nodez = nodes.item(1);
              String s = nodez.getFirstChild().getNodeName(); //outputs #text?
                          System.out.println(s);
                          //If I try this
                          String s = nodez.getNodeName(); //outputs 'stock'
  
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  
  
  }
  </code></pre>
  
  <p>So if I try to output current node name, it outputs properly, but if I try to output next child node name, it just outputs #text.
  How can I properly output any node I want? I thought I could just use method as <code>getFirstChild</code> or <code>getNextSibling</code>, but it seems I'm doing something wrong?</p>
  
  <p>You can find xml file using this link:</p>
  
  <p><a href="http://javarevisited.blogspot.com/2011/12/parse-xml-file-in-java-example-tutorial.html" rel="nofollow">http://javarevisited.blogspot.com/2011/12/parse-xml-file-in-java-example-tutorial.html</a></p>