• Advertisement
Sign in to follow this  

[java] XML output - Indent problem

This topic is 4123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
 // 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/xslt}indent-amount", "4");
 t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream("Test.xml")));
} catch(Exception e) {
 // ...

<?xml version="1.0" encoding="UTF-8"?>
<C>Indent Test</C>

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement