title: XML text extraction
body: <p><strong>Scenario:</strong></p>
  
  <p>Given the following XML file:</p>
  
  <pre><code>&lt;a:root
  xmlns:h="http://www.w3.org/TR/html4/"
  xmlns:f="http://www.w3schools.com/furniture"&gt;
  
  &lt;h:table&gt;
    &lt;h:tr&gt;
      &lt;h:td&gt;Apples&lt;/h:td&gt;
      &lt;h:td&gt;Bananas&lt;/h:td&gt;
    &lt;/h:tr&gt;
  &lt;/h:table&gt;
  
  &lt;f:table&gt;
    &lt;f:name&gt;African Coffee Table&lt;/f:name&gt;
    &lt;f:width&gt;80&lt;/f:width&gt;
    &lt;f:length&gt;120&lt;/f:length&gt;
  &lt;/f:table&gt;
  
  aaaaaaaaaaaaaa
  
  &lt;/a:root&gt;
  </code></pre>
  
  <p>How do I extract the text inside the main element <code>&lt;a:root&gt;</code>: </p>
  
  <pre><code>"\naaaaaaaaaaaaaa\n"
  </code></pre>
  
  <p>The code I have right now is:</p>
  
  <pre><code>import java.io.File;
  import java.util.Stack;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.NodeList;
  
  
  public class Proof {
      public static void main(String[] args) {
          Document doc = null;
          DocumentBuilderFactory dbf = null;
          DocumentBuilder docBuild = null;
          try {
  
              dbf = DocumentBuilderFactory.newInstance();
              docBuild = dbf.newDocumentBuilder();
              doc = docBuild.parse(new File("test2.xml"));
  
              System.out.println(doc.getFirstChild().getTextContent());
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>But it returns the text I desire ("aaaaaaaaaaaaaa") + the inner text for the rest of the elements . Output:</p>
  
  <pre><code>    Apples
      Bananas
  
  
  
  
    African Coffee Table
    80
    120
  
  
  aaaaaaaaaaaaaa
  </code></pre>
  
  <p>The requirement is <strong>not</strong> to use an additional XML java library !</p>