title: Correct namespace declaration in Java
body: <p>I need to create an XML document with the following structure: </p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt; 
  &lt;Cancelacion xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               RfcEmisor="VSI850514HX4" 
               Fecha="2011-11-23T17:25:06" 
               xmlns="http://cancelacfd.sat.gob.mx"&gt;
      &lt;Folios&gt;
          &lt;UUID&gt;BD6CA3B1-E565-4985-88A9-694A6DD48448&lt;/UUID&gt; 
      &lt;/Folios&gt;
  &lt;/Cancelacion&gt;
  </code></pre>
  
  <p>The structure MUST be that way. But I'm not very familiar with the namespace declaration for the XML Elements. I can correctly generate an XML with the following structure: </p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt; 
  &lt;Cancelacion RfcEmisor="VSI850514HX4" 
               Fecha="2011-11-23T17:25:06" 
               xmlns="http://cancelacfd.sat.gob.mx"&gt;
      &lt;Folios&gt;
          &lt;UUID&gt;BD6CA3B1-E565-4985-88A9-694A6DD48448&lt;/UUID&gt; 
      &lt;/Folios&gt;
  &lt;/Cancelacion&gt;
  </code></pre>
  
  <p><br>
  But the problem is that I can't get to include the <code>xmls:xsd</code> and <code>xmlns:xsi</code> correctly. The code for properly generating the previously mentioned code is: </p>
  
  <pre><code>// Crear un document XML vacío
  DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
  dbfac.setNamespaceAware(true);
  DocumentBuilder docBuilder;
  docBuilder = dbfac.newDocumentBuilder();
  Document doc = docBuilder.newDocument();
  doc.setXmlVersion("1.0");
  doc.setXmlStandalone(true);
  
  Element cancelacion = doc.createElementNS("http://cancelacfd.sat.gob.mx","Cancelacion");
  cancelacion.setAttribute("RfcEmisor", rfc);
  cancelacion.setAttribute("Fecha", fecha);
  doc.appendChild(cancelacion);
  
  Element folios = doc.createElementNS("http://cancelacfd.sat.gob.mx", "Folios");
  cancelacion.appendChild(folios);
  for (int i=0; i&lt;uuid.length; i++) {
      Element u = doc.createElementNS("http://cancelacfd.sat.gob.mx","UUID");
      u.setTextContent(uuid[i]);
      folios.appendChild(u);
  }
  </code></pre>