Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[java] XML output - Indent problem

Started by CodeMachine Oct 6, 2006 at 1:26 AM 0 replies 750+ views
Original Post
CodeMachine
CodeMachine
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:/&#47;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>
&lt;?xml version=<span class="cpp-literal">"1.0"</span> encoding=<span class="cpp-literal">"UTF-8"</span>?&gt;
&lt;A&gt;
&lt;B&gt;
&lt;C&gt;Indent Test&lt;/C&gt;
&lt;/B&gt;
&lt;/A&gt;


</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>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.