Question
How can I pretty-print XML output with correct indentation and doctype positioning using only the standard Java API?
package test;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class TestOutputKeys {
public static void main(String[] args) throws TransformerException {
Source xmlInput = new StreamSource(new StringReader(
"<!-- Document comment --><aaa><bbb/><ccc/></aaa>"));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("indent-amount", "4"); // Set indentation level
transformer.transform(xmlInput, xmlOutput);
System.out.println(xmlOutput.getWriter().toString());
}
}
Answer
To pretty-print XML using the standard Java API while ensuring correct indentation and proper doctype positioning, adjustments must be made in the Transformer properties. Here's a detailed process to achieve the desired output.
transformer.setOutputProperty("indent-amount", "4"); // Configure indentation level.
Causes
- The doctype declaration often appears after the comment section due to how the Transformer processes XML. Unfortunately, there is no direct property to control doctype positioning.
- By default, indentation may not function as expected due to missing configuration parameters.
Solutions
- Set the `indent-amount` property to define the number of spaces used for indentation. It controls the visual structure of the XML output.
- To manage the doctype positioning, certain custom solutions or manipulations are necessary, as the standard API does not allow for explicit control over this aspect.
Common Mistakes
Mistake: Failing to set the `indent-amount` property, leading to no visible indentation in the output.
Solution: Always specify the `indent-amount` property after enabling indentation.
Mistake: Expecting the doctype to be placed before comments without additional configuration or filtering.
Solution: Consider post-processing your XML string to rearrange the doctype declaration if standard API features do not support it.
Helpers
- Java pretty-print XML
- pretty-print XML Java API
- Java XML indentation
- Java Transformer API
- XML doctype positioning in Java