title: How to add namespace prefix in Java DOM XML builder
body: <p>I need to set prefix of the element in my xml. I need to print the xml in the format below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;cars xmlns:dcterms="http://purl.org/dc/terms/" xmlns:msxsl="urn:schemas-microsoft-com:xslt"&gt;
  &lt;supercars company="Ferrari"&gt;
  &lt;dcterms:carname type="formula one"&gt;Ferrari 101&lt;/dcterms:carname&gt;
  &lt;msxsl:carname type="sports"&gt;Ferrari 202&lt;/msxsl:carname&gt;
  &lt;/supercars&gt;
  &lt;/cars&gt;
  </code></pre>
  
  <p>However, I am not able to add the prefix in my XML. What I am getting is this.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;cars xmlns:dcterms="http://purl.org/dc/terms/" xmlns:msxsl="urn:schemas-microsoft-com:xslt"&gt;
  &lt;supercars company="Ferrari"&gt;
  &lt;carname type="formula one"&gt;Ferrari 101&lt;/carname&gt;
  &lt;carname type="sports"&gt;Ferrari 202&lt;/carname&gt;
  &lt;/supercars&gt;
  &lt;/cars&gt;
  </code></pre>
  
  <p>Following is my Java Code:</p>
  
  <pre><code>public static void main(String args[]) {
  
        try {
           DocumentBuilderFactory dbFactory =
           DocumentBuilderFactory.newInstance();
           DocumentBuilder dBuilder = 
              dbFactory.newDocumentBuilder();
  
           Document doc = dBuilder.newDocument();
  
           // root element
  
           Element rootElement = doc.createElement("cars");
           rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:dcterms", "http://purl.org/dc/terms/");
           rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:msxsl", "urn:schemas-microsoft-com:xslt");
           doc.appendChild(rootElement);
  
  
           //  supercars element
           Element supercar = doc.createElement("supercars");
  
           rootElement.appendChild(supercar);
  
           // setting attribute to element
           Attr attr = doc.createAttribute("company");
           attr.setValue("Ferrari");
         //  supercar.setAttributeNodeNS(attr);
           supercar.setAttributeNode(attr);
  
           // carname element
           Element carname = doc.createElement("carname");
           Attr attrType = doc.createAttribute("type");
           attrType.setValue("formula one");
           carname.setAttributeNode(attrType);
           carname.appendChild(
           doc.createTextNode("Ferrari 101"));
           supercar.appendChild(carname);
  
           Element carname1 = doc.createElement("carname");
           Attr attrType1 = doc.createAttribute("type");
           attrType1.setValue("sports");
           carname1.setAttributeNode(attrType1);
           carname1.appendChild(
           doc.createTextNode("Ferrari 202"));
           supercar.appendChild(carname1);
  
           // write the content into xml file
           TransformerFactory transformerFactory =
           TransformerFactory.newInstance();
           Transformer transformer =
           transformerFactory.newTransformer();
           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
           transformer.setOutputProperty(OutputKeys.INDENT, "yes");
           transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 
           DOMSource source = new DOMSource(doc);
           StreamResult result =
           new StreamResult(new File("cars.xml"));
           transformer.transform(source, result);
           // Output to console for testing
           StreamResult consoleResult =
           new StreamResult(System.out);
           transformer.transform(source, consoleResult);
        } catch (Exception e) {
           e.printStackTrace();
        }
     }
  </code></pre>
  
  <p>}</p>
  
  <p>And I checked this link <a href="https://stackoverflow.com/questions/13501907/adding-namespace-prefix-xml-string-using-xml-dom">Adding namespace prefix XML String using XML DOM</a>. However, this only talks about the SAX way of doing thing. I need to do this in DOM.</p>
  
  <p>I tried setPrefix method and it is throwing me NAMESPACE_ERR</p>