title: Parsing xml in java - getTextContent() and getNodeValue() only return \n, \t, and whitespace
body: <p>I'm having a little trouble with parsing some XML in java.  My XML file is read correctly and I'm able to obtain most information from the file without any trouble (ie. the StreamType node, shown in the xml snippet), using the nodes' getTextContent() function.</p>
  
  <p>BUT, when I try to work with children of a node, both getNodeValue() and getTextContent() return this random value:  "\n                \t\t".  </p>
  
  <p>The NodeList propertyNodes seems to be correctly populated (contains all 18 "Property" elements).</p>
  
  <p>Here's a snippet from my code:</p>
  
  <pre><code>Document document;
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  document = builder.parse(appXML);
  
  ...
  
  String typeName = document.getElementsByTagName("StreamType").item(0).getTextContent();
  
  ...
  
  String limit = "-1";
  NodeList propertyNodes = document.getElementsByTagName("Property");
  int nodelistlength = propertyNodes.getLength();
  for (int i = 0; i &lt; nodelistlength; i++) {
      Node currentNode = propertyNodes.item(i);
      Node nameNode = currentNode.getFirstChild();
      Node valueNode = currentNode.getLastChild();
  
      String name = nameNode.getNodeValue();
      String value = valueNode.getNodeValue();
  
      if (nameNode.getTextContent().equalsIgnoreCase("maxConnections"))
          limit = valueNode.getTextContent();
  }
  </code></pre>
  
  <p>And here's some snips from the XML I'm trying to parse:</p>
  
  <pre><code>&lt;Root&gt;
   &lt;Application&gt; 
    &lt;Streams&gt;
     &lt;StreamType&gt;live&lt;/StreamType&gt;
     ...
    &lt;/Streams&gt;
  
    ...
  
    &lt;Properties&gt; 
  
     ...
  
     &lt;Property&gt;
      &lt;Name&gt;maxConnections&lt;/Name&gt; 
      &lt;Value&gt;1000&lt;/Value&gt; 
     &lt;/Property&gt; 
  
     ...
  
    &lt;/Properties&gt;
   &lt;/Application&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Any idea what I might be doing wrong here?  Thanks very much!</p>
  
  <p><strong>EDIT:  Works now, thanks to the tutorial posted by @home.  This is how I fixed the code:</strong></p>
  
  <p>1)  Modified the block of code starting with "String limit" and ending with the for loop's end bracket:</p>
  
  <pre><code>String limit = "-1";
  NodeList propertyNodes = document.getElementsByTagName("Property");
  for (int i = 0; i &lt; propertyNodes.getLength(); i++) {
      Node currentNode = propertyNodes.item(i);
  
      if (currentNode.getNodeType() != Node.ELEMENT_NODE)
          continue;
  
      Element currentElement = (Element)currentNode;
  
      if (getTagValue("Name",currentElement).equalsIgnoreCase("maxConnections"))
          limit = getTagValue("Value",currentElement);
  }
  </code></pre>
  
  <p>2) Added this handy function from the tutorial:</p>
  
  <pre><code>private static String getTagValue(String sTag, Element eElement) {
      NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
      Node nValue = (Node) nlList.item(0);
      return nValue.getNodeValue();
  }
  </code></pre>
  
  <p>It fetches the child name/value pairs of the "Properties" element flawlessly now.  Thanks very much!</p>