Original Post
Hi I've problem indenting the XML while saving a Document to file. The resulting XML is not indented. Code:
try {
// Create document.
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
// Create nodes.
Element a = doc.createElement("A");
Element b = doc.createElement("B");
Element c = doc.createElement("C");
c.setTextContent("Indent Test");
// Append nodes.
doc.appendChild(a);
a.appendChild(b);
b.appendChild(c);
// Save to file.
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty("method", "xml");
t.setOutputProperty("encoding", "UTF-8");
t.setOutputProperty("indent", "yes");
t.setOutputProperty("{http://xml.apache.org&#<span class="java-number">47</span>;xslt}indent-amount"</span>, <span class="java-literal">"<span class="java-number">4</span>"</span>);
t.transform(<span class="java-keyword">new</span> DOMSource(doc), <span class="java-keyword">new</span> StreamResult(<span class="java-keyword">new</span> FileOutputStream(<span class="java-literal">"Test.xml"</span>)));
} <span class="java-keyword">catch</span>(Exception e) {
<span class="java-comment">// ...</span>
}
</pre></div><!--ENDSCRIPT-->
Result:
<!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre>
<?xml version=<span class="cpp-literal">"1.0"</span> encoding=<span class="cpp-literal">"UTF-8"</span>?>
<A>
<B>
<C>Indent Test</C>
</B>
</A>
</pre></div><!--ENDSCRIPT-->
Why isn't the result indented?
I've set properties to the Transformer, both "indent" and "indent-amount", but I have no indent at all. Why?
Do I manually have to traverse the Document and print all data to a file myself?
/ Kind Regards</code></pre></div>