title: Getting XML Node text value with Java DOM
body: <p>I can't fetch text value with <code>Node.getNodeValue()</code>, <code>Node.getFirstChild().getNodeValue()</code> or with <code>Node.getTextContent()</code>.</p>
  
  <p>My XML is like </p>
  
  <pre><code>&lt;add job="351"&gt;
      &lt;tag&gt;foobar&lt;/tag&gt;
      &lt;tag&gt;foobar2&lt;/tag&gt;
  &lt;/add&gt;
  </code></pre>
  
  <p>And I'm trying to get <em>tag</em> value (non-text element fetching works fine). My Java code sounds like</p>
  
  <pre><code>Document doc = db.parse(new File(args[0]));
  Node n = doc.getFirstChild();
  NodeList nl = n.getChildNodes();   
  Node an,an2;
  
  for (int i=0; i &lt; nl.getLength(); i++) {
      an = nl.item(i);
  
      if(an.getNodeType()==Node.ELEMENT_NODE) {
          NodeList nl2 = an.getChildNodes();
  
          for(int i2=0; i2&lt;nl2.getLength(); i2++) {
              an2 = nl2.item(i2);
  
              // DEBUG PRINTS
              System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
  
              if(an2.hasChildNodes())
                  System.out.println(an2.getFirstChild().getTextContent());
  
              if(an2.hasChildNodes())
                  System.out.println(an2.getFirstChild().getNodeValue());
  
              System.out.println(an2.getTextContent());
              System.out.println(an2.getNodeValue());
          }
      }
  }
  </code></pre>
  
  <p>It prints out</p>
  
  <pre><code>tag type (1): 
  tag1
  tag1
  tag1
  null
  #text type (3):
  _blank line_
  _blank line_
  ...
  </code></pre>
  
  <p>Thanks for the help.</p>