title: Why am I getting extra text nodes as child nodes of root node?
body: <p>I want to print the child elements of the root node. This is my XML file.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!-- Comment--&gt;
  &lt;company&gt;
     &lt;staff id="1001"&gt;
         &lt;firstname&gt;yong&lt;/firstname&gt;
         &lt;lastname&gt;mook kim&lt;/lastname&gt;
         &lt;nickname&gt;mkyong&lt;/nickname&gt;
         &lt;salary&gt;100000&lt;/salary&gt;
     &lt;/staff&gt;
     &lt;staff id="2001"&gt;
         &lt;firstname&gt;low&lt;/firstname&gt;
         &lt;lastname&gt;yin fong&lt;/lastname&gt;
         &lt;nickname&gt;fong fong&lt;/nickname&gt;
         &lt;salary&gt;200000&lt;/salary&gt;
     &lt;/staff&gt;
  &lt;/company&gt;
  </code></pre>
  
  <p>According to my understanding, root node is 'company' and its child nodes must be 'staff' and 'staff' (as there are 'staff' nodes 2 times). But when I am trying to get them through my java code I am getting 5 child nodes. Where are the 3 extra text nodes coming from?</p>
  
  <p>Java Code:</p>
  
  <pre><code>package com.training.xml;
  
  import java.io.File;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class ReadingXML {
  
  public static void main(String[] args) {
      try {
  
          File file = new File("D:\\TestFile.xml");
  
          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
          Document doc = dBuilder.parse(file);
          doc.getDocumentElement().normalize();
  
          System.out.println("root element: " + doc.getDocumentElement().getNodeName());
  
          Node rootNode = doc.getDocumentElement(); 
          System.out.println("root: " + rootNode.getNodeName());
  
          NodeList nList = rootNode.getChildNodes(); 
  
          for(int i = 0; i &lt; nList.getLength(); i++) {
              System.out.println("node name: " + nList.item(i).getNodeName() );
          }           
      } catch(Exception e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>root element: company
  root: company
  node name: #text
  node name: staff
  node name: #text
  node name: staff
  node name: #text
  </code></pre>
  
  <p>Why the three text nodes are coming over here?</p>