title: Adding namespace to an already created XML document
body: <p>I am creating a W3C Document object using a String value. Once I created the Document object, I want to add a namespace to the root element of this document. Here's my current code:</p>
  
  <pre><code>Document document = builder.parse(new InputSource(new StringReader(xmlString)));
  document.getDocumentElement().setAttributeNS("http://com", "xmlns:ns2", "Test");
  document.setPrefix("ns2");
  TransformerFactory tranFactory = TransformerFactory.newInstance();
  Transformer aTransformer = tranFactory.newTransformer();
  Source src = new DOMSource(document);
  Result dest = new StreamResult(new File("c:\\xmlFileName.xml"));
  aTransformer.transform(src, dest);
  </code></pre>
  
  <p>What I use as input: </p>
  
  <pre><code>&lt;product&gt;
      &lt;arg0&gt;DDDDDD&lt;/arg0&gt;
      &lt;arg1&gt;DDDD&lt;/arg1&gt;
  &lt;/product&gt;
  </code></pre>
  
  <p>What the output should look like: </p>
  
  <pre><code>&lt;ns2:product xmlns:ns2="http://com"&gt;
      &lt;arg0&gt;DDDDDD&lt;/arg0&gt;
      &lt;arg1&gt;DDDD&lt;/arg1&gt;
  &lt;/ns2:product&gt;
  </code></pre>
  
  <p>I need to add the prefix value and namespace also to the input xml string. If I try the above code I am getting this exception: </p>
  
  <pre><code>NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
  </code></pre>
  
  <p>Appreciate your help!</p>