How to pretty print XML from Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Pretty-printing XML in Java usually means taking compact markup and writing it back with indentation and line breaks so humans can read it comfortably. The standard Java solution is to use a Transformer, and the right details are mostly about choosing the input form, preserving encoding expectations, and understanding that formatting can change whitespace presentation.
The Standard Approach: Transformer
If your XML is currently a string, you can feed it into a JAXP transformer and request indentation.
For many everyday Java programs, this is enough.
If You Already Have a DOM Document
If your code is already working with a parsed DOM tree, use DOMSource instead of reparsing from a string.
This is cleaner when the XML has already been parsed and modified in memory.
Parsing First Can Improve Robustness
If the input XML might be malformed, parsing it first gives you a clearer failure mode than sending a broken string straight into a transform pipeline.
Then you can pretty-print the resulting Document using the earlier method.
Pretty Printing Is for Humans, Not Semantic Changes
Formatting changes do not usually alter XML meaning, but they can affect whitespace presentation in certain content models. That matters when text nodes intentionally contain significant spaces or line breaks.
So pretty printing is best used for:
- logging
- debugging
- config display
- developer-facing output
Be more cautious when rewriting XML that is sensitive to exact whitespace representation.
Writing to a File
Pretty printing often ends with writing the formatted output to disk.
If you care about encoding, set the transformer output property and write the file using a matching character set strategy.
Third-Party Libraries Exist, but Start Simple
Libraries such as JDOM, DOM4J, and others can also format XML, but you do not need an extra dependency just to indent XML in a typical Java application. The built-in transformer approach is usually enough unless your XML-processing stack already relies on a different library.
Common Pitfalls
The biggest mistake is assuming pretty printing is always semantically harmless. In many cases it is, but whitespace-sensitive XML content deserves more care.
Another issue is forgetting that the input must still be well-formed XML. Pretty printing does not repair malformed markup.
Developers also sometimes mix string-based transformation and DOM-based transformation awkwardly. If you already have a Document, use DOMSource instead of converting back and forth unnecessarily.
Finally, do not hardcode formatting expectations too rigidly in tests. Different transformer implementations can vary slightly in the exact output layout while still producing valid indented XML.
Summary
- In Java, pretty-printing XML is usually done with a
Transformer. - Use
StreamSourcefor XML strings andDOMSourcefor existingDocumentobjects. - Indentation is controlled through transformer output properties.
- Pretty printing is ideal for human readability, not for repairing malformed XML.
- Be cautious when exact whitespace in XML content is significant.

