Format and Pretty-Print XML Online
Format raw or minified XML into human-readable, properly indented markup. Learn how pretty-printing works, indentation options, and best practices for readable XML documents.
Detailed Explanation
Pretty-Printing XML
Pretty-printing transforms compact or single-line XML into a well-structured, indented document that is easy for humans to read and edit. The formatter parses the XML into a DOM tree and then serializes it back with consistent indentation and line breaks.
How Pretty-Printing Works
Given a compact XML string:
<catalog><book id="1"><title>XML Developer's Guide</title><price>44.95</price></book></catalog>
The formatter produces:
<catalog>
<book id="1">
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
</catalog>
Each nested element is indented by a consistent amount (commonly 2 or 4 spaces), and each opening/closing tag pair gets its own line when the element contains children.
Indentation Options
Most formatters offer several indentation styles:
- 2 spaces — compact, popular in web development and JavaScript ecosystems
- 4 spaces — more visually distinct hierarchy, common in Java and enterprise XML
- Tab characters — preferred by some teams for accessibility and configurability
- 1 space — rarely used, but minimizes file size while maintaining readability
Attribute Formatting
When elements have many attributes, formatters can optionally place each attribute on its own line:
<element
attr1="value1"
attr2="value2"
attr3="value3" />
This is especially useful for configuration files where elements carry many attributes.
Preserving Content
A good formatter preserves text content exactly as it appears. Mixed-content elements (elements containing both text and child elements) require careful handling to avoid introducing unwanted whitespace into the text.
Self-Closing Tags
Empty elements like <br/> or <item/> can be formatted as self-closing tags or as explicit open/close pairs (<item></item>). Most formatters default to self-closing for brevity.
Use Case
XML pretty-printing is essential when reviewing configuration files, debugging API responses (SOAP, REST/XML), inspecting build files (Maven POM, Ant), or preparing XML for documentation and code reviews. Formatted XML is dramatically easier to diff in version control systems.
Try It — XML Formatter
Related Topics
Minify XML — Remove Whitespace and Reduce File Size
Basic Formatting
XML Indentation Styles — Spaces, Tabs, and Depth Control
Basic Formatting
Format SOAP XML Messages — Envelope, Header, and Body
XML Types
Find and Fix Common XML Syntax Errors
Validation
XML Entity References — Predefined, Numeric, and Custom Entities
Advanced