title: Why is this java code not parsing my Xml correctly?
body: <p>I've written class to parse some xml into an object and it's not working correctly, when I try and get the value of a node I get a null rather than the contents of the node.</p>
  
  <p>Here is a simplified version of my class that just does the xml parsing of a single node:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.apache.log4j.Logger;
  import org.apache.log4j.xml.DOMConfigurator;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.SAXException;
  
  public class XmlReaderCutDown {
  
      private static Logger logger = Logger.getLogger(CtiButtonsXmlReader.class);
  
      public static void testXml(String confFile){
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
          try {
              DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
              Document doc = docBuilder.parse(new File(confFile));
              doc.getDocumentElement().normalize();
              if (logger.isDebugEnabled()){
                  logger.debug("The root element is " + doc.getDocumentElement().getNodeName());
              }
  
              NodeList rows =  doc.getElementsByTagName("row");
  
              NodeList topRowButtons = ((Element)rows.item(0)).getElementsByTagName("button");
              logger.debug("Top row has " +topRowButtons.getLength() + " items.");
              Node buttonNode = topRowButtons.item(0);
  
              NodeList nodeList = ((Element)buttonNode).getElementsByTagName("id");
              logger.debug("Node list count for "+ "id" + " = " + nodeList.getLength());
              Element element = (Element)nodeList.item(0);
              String xx = element.getNodeValue();
              logger.debug(xx);
              String elementValue = ((Node)element).getNodeValue();
              if (elementValue != null) {
                  elementValue = elementValue.trim();
              }
              else {
                  logger.debug("Value was null");
              }
              logger.debug("Node id = "+ elementValue);
  
          } catch (ParserConfigurationException e) {
  
              logger.fatal(e.toString(),e);
          } catch (SAXException e) {
              logger.fatal(e.toString(),e);
          } catch (IOException e) {
              logger.fatal(e.toString(),e);
          } catch (Exception e) {
              logger.fatal(e.toString(),e);
          }
  
      }
  
  
      public static void main(String[] args){
          DOMConfigurator.configure("log4j.xml");
  
          testXml("test.xml");
      }
  }
  </code></pre>
  
  <p>And here is a stripped down version of my xml file:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;root&gt;
      &lt;row&gt;
          &lt;button&gt;
              &lt;id&gt;this is an id&lt;/id&gt;
              &lt;action&gt;action&lt;/action&gt;
              &lt;image-src&gt;../images/img.png&lt;/image-src&gt;
              &lt;alt-text&gt;alt txt&lt;/alt-text&gt;
              &lt;tool-tip&gt;Tool tip&lt;/tool-tip&gt;
          &lt;/button&gt;
      &lt;/row&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>This is what the logging statments output:</p>
  
  <p>DEBUG XmlReaderCutDown - The root element is root</p>
  
  <p>DEBUG XmlReaderCutDown - Top row has 1 items.</p>
  
  <p>DEBUG XmlReaderCutDown - Node list count for id = 1</p>
  
  <p>DEBUG XmlReaderCutDown - </p>
  
  <p>DEBUG XmlReaderCutDown - Value was null</p>
  
  <p>DEBUG XmlReaderCutDown - Node id = null</p>
  
  <p>Why isn't it getting the text in the xml node?</p>
  
  <p>I'm running using JDK 1.6_10</p>