title: Java 'getLocalName()' returns null even with 'setNamespaceAware(true)'
body: <p>I have the following sample xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root attr1="value1"/&gt;
  </code></pre>
  
  <p>The following Java sample demonstrates the issue I am facing:</p>
  
  <pre><code>import java.io.FileInputStream;
  import java.io.InputStream;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Attr;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NamedNodeMap;
  
  public class XMLClass {
  
  public static void main(String[] args) throws Exception {
      // path to xml file
      String filename = "src/resources/xmlfile.xml";
  
      DocumentBuilderFactory db = DocumentBuilderFactory.newInstance();
  
      // this only helps for attr1 but not attr2
      db.setNamespaceAware(true);
  
      InputStream input = new FileInputStream(filename);
      Document doc = db.newDocumentBuilder()
              .parse(input);
  
      Element root = doc.getDocumentElement();
  
      // create an additional attribute
      root.setAttribute("attr2", "value2");
  
      NamedNodeMap nnm = root.getAttributes();
  
      // The attribute name and value is correct
      // for the attr1, however, the name for
      // attr2 is null
      for (int i = 0; i &lt; nnm.getLength(); i++) {
          Attr a = (Attr) nnm.item(i);
          String name = a.getLocalName();
          String value = a.getValue();
  
          System.out.println("name: " + name + "; value: " + value);
      }
  
      System.exit(0);
  }
  
  
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>name: attr1; value: value1
  name: null; value: value2
  </code></pre>
  
  <p>I have searched the Web and the only advise I found was to use <code>setNamespaceAware(true)</code>, as I had already done in my code. This ensures that the attribute name for <code>attr1</code>, which is defined in the XML file, is returned correctly by <code>getLocalName()</code>. However, the attribute name <code>attr2</code>, which is set in the code via <code>setAttribute()</code> is null, although the value is correctly retrieved.</p>
  
  <p>What is the reason for this behaviour and what is the proper way to solve my issue?</p>