title: Java Document builder returning null Document for Xml String
body: <p><a href="https://i.stack.imgur.com/o4fxi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/o4fxi.png" alt="enter image description here"></a>I have tried all possible <a href="https://stackoverflow.com/questions/33098082/passing-xml-string-as-document-returning-null">answers</a> on Stackoverflow but I can't get this resolved.</p>
  
  <p>I have the following xml as a String:</p>
  
  <pre><code>private static final String xmlStr = "&lt;parameters&gt;\n" +
              "  &lt;parameter&gt;\n" +
              "    &lt;name&gt;emp&lt;/name&gt;\n" +
              "    &lt;keyvalue&gt;John Smith&lt;/keyvalue&gt;\n" +
              "  &lt;/parameter&gt;\n" +
              "  &lt;parameter&gt;\n" +
              "    &lt;name&gt;age&lt;/name&gt;\n" +
              "    &lt;keyvalue&gt;22&lt;/keyvalue&gt;\n" +
              "  &lt;/parameter&gt;\n" +
              "  &lt;parameter&gt;\n" +
              "    &lt;name&gt;Birth Date&lt;/name&gt;\n" +
              "    &lt;keyvalue&gt;02/05/1978&lt;/keyvalue&gt;\n" +
              "  &lt;/parameter&gt;\n" +
              "  &lt;parameter&gt;\n" +
              "    &lt;name&gt;password&lt;/name&gt;\n" +
              "    &lt;keyvalue&gt;ye63633&lt;/keyvalue&gt;\n" +
              "  &lt;/parameter&gt;\n" +
              "&lt;/parameters&gt;";
  </code></pre>
  
  <p>The parameters can be replaced with any string (which I tried without sucess), and the following code returns null document:</p>
  
  <pre><code> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true);
              DocumentBuilder builder = factory.newDocumentBuilder();
              Document document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
  </code></pre>
  
  <p>I have tried replacing document with: </p>
  
  <pre><code>Document document = builder.parse(new InputSource(new StringReader(xmlStr)));//This used to work 2 weeks ago.
  </code></pre>
  
  <p><strong>document</strong> is returning null document all the time and I can't figure out what I am doing wrong. The same code used to work few weeks ago.</p>
  
  <p>I am using Java 1.8 !!</p>
  
  <p>I appericiate your help.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here is the full code that takes the above <strong>xmlStr</strong> and try to access age of the employee.</p>
  
  <pre><code>private String getElementValue(String tagName, String xmlString) {
  
          try {
  
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              DocumentBuilder builder = factory.newDocumentBuilder();
              Document document = builder.parse(new InputSource(new StringReader(xmlString)));
              Element rootElement = document.getDocumentElement();
  
              NodeList list = rootElement.getElementsByTagName(tagName);
  
              if (list != null &amp;&amp; list.getLength() &gt; 0) {
                  NodeList subList = list.item(0).getChildNodes();
  
                  if (subList != null &amp;&amp; subList.getLength() &gt; 0) {
                      return subList.item(0).getNodeValue();
                  }
              }
          } catch (ParserConfigurationException e) {
              e.printStackTrace();
          } catch (SAXException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
          return null;
      }
  </code></pre>
  
  <p>Based on the comments, the document is not null at all but there is an issue accessing the desired xml tag. So the code <code>NodeList list = rootElement.getElementsByTagName("age");</code>
  has size 0. i.e its not entering the loop at all.</p>