Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why should the document have your proposed structure? You're declaring namespaces with prefixes, but your example output doesn't include any elements in those namespaces. Those declarations are therefore unnecessary. </p> <p>First, understand that <code>xmlns</code> (or <code>xmlns:prefix</code>) is the reserved XML pseudo-attribute for declaring namespaces. It's not a normal attribute. Second, the location of namespace declarations in the document shouldn't concern you, as long as you're creating elements in the desired namespaces in the first place. </p> <p><em>Let the serializer decide where to place the namespace declarations.</em></p> <p>Register an element in the correct namespace like this:</p> <pre><code>Element cancelacion = doc.createElementNS( "http://cancelacfd.sat.gob.mx", "Cancelacion"); doc.appendChild(cancelacion); Element child = doc.createElementNS("http://cancelacfd.sat.gob.mx", "SomeChild"); cancelacion.appendChild(child); </code></pre> <p>When serialized:</p> <pre><code>DOMSource domSource = new DOMSource(doc); StreamResult streamResult = new StreamResult(System.out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.transform(domSource, streamResult); </code></pre> <p>Result:</p> <pre><code>&lt;Cancelacion xmlns="http://cancelacfd.sat.gob.mx"&gt; &lt;SomeChild/&gt; &lt;/Cancelacion&gt; </code></pre> <p>Notice that <code>Cancelacion</code> and <code>SomeChild</code> were created in exactly the same way, but only <code>Cancelacion</code> contains the namespace declaration (because the declaration applies for all descendants). The serializer handled this for us. </p> <p><strong>Warning:</strong> <em>What follows is a hack. I do not recommend using it. It will probably get you into trouble. You should probably stop reading. But...if you have no choice, it might work.</em></p> <p>If you're desperate, you could manually splice in the unused namespaces. (Treating XML as a string is almost always a bad idea.)</p> <p>First, save the result in an <code>OutputStream</code> that can be converted to a <code>String</code>:</p> <pre><code>ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(doc); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); </code></pre> <p>Then jam the namespace declarations in there without any regard for what is good, right, and decent:</p> <pre><code>String[] parts = out.toString().split("\\\"&gt;", 2); String result = parts[0] + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"&gt;" + parts[1]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload