What is an XML formatter?
An XML formatter takes XML that's hard to read, usually because it's minified onto one line or inconsistently indented, and re-prints it with one element per line and indentation that shows the nesting. This one parses your input with the browser's native XML parser first, so it also tells you when the document isn't well-formed.
XML (Extensible Markup Language) is defined by the W3C's XML 1.0 specification. It's still common in SOAP APIs, RSS and Atom feeds, sitemaps, Android layouts, Maven pom.xml files and office document formats.
How to format XML
- Paste your XML into the input box.
- Click Format.
- Read or copy the indented result. If there's a syntax error, the error message replaces the output.
Worked example
This one-line catalog:
<?xml version="1.0" encoding="UTF-8"?><catalog><!-- spring list --><book id="bk101" lang="en"><title>XML Basics</title><price currency="USD">29.99</price><instock/></book></catalog>
is formatted as:
<catalog>
<!-- spring list -->
<book id="bk101" lang="en">
<title>XML Basics</title>
<price currency="USD">29.99</price>
<instock/>
</book>
</catalog>
Notice what happened along the way. Elements with only text (<title>, <price>) stay on one line. The empty <instock/> stays self-closing, and an empty pair like <instock></instock> would be written that way too. The comment is kept. The <?xml ...?> declaration at the top is gone.
How it works and what that means
The formatter uses DOMParser with the application/xml type, which is a strict parser: one unclosed tag or stray & and the whole document is rejected, as the XML spec requires. If parsing succeeds, the tool walks the element tree and prints each node with two spaces per level.
That walk only knows about elements, comments and text, which leads to some limits you should know before you save the output anywhere:
| Input | What the formatter does |
|---|---|
<?xml ...?>, <!DOCTYPE ...>, <?pi ...?> before the root |
Left out of the output |
<b>Tom & Jerry</b> |
Printed as <b>Tom & Jerry</b>, which is invalid XML |
note="say "hi"" |
Printed as note="say "hi"", also invalid |
<a><![CDATA[x < y]]></a> |
CDATA content dropped, printed as <a/> |
<p>Hello <b>world</b> again</p> |
Text beside child elements dropped: only <b>world</b> remains |
<a> spaced text </a> |
Leading and trailing spaces trimmed, inner spaces kept |
xmlns:x="urn:x" and <x:item> |
Kept as written |
In short, it's dependable for data-style XML, where every element holds either child elements or a single text value: API responses, config files, feeds and sitemaps without CDATA or escaped characters. For document-style XML with mixed content, or anything containing &, < or CDATA, use the output only as a reading aid and keep your original file.
Well-formed is not the same as valid
The parser checks that your XML is well-formed: tags are closed and properly nested, attributes are quoted, there's a single root element, and special characters are escaped. It doesn't check the document against a schema (XSD) or DTD, so a well-formed file can still be missing a required element. Common well-formedness errors:
| Mistake | Fix |
|---|---|
<a><b></a> |
Close <b> before </a> |
<item id=5> |
Quote attribute values: id="5" |
<name>R&D</name> |
Escape the ampersand: R&D |
| Two top-level elements | Wrap them in a single root element |
Text before <?xml ...?> |
The declaration must be the very first thing in the file |
Related tools
- XML editor: edit XML with syntax highlighting.
- JSON formatter: the same job for JSON responses.
- Code diff: compare two formatted XML files line by line.
- Code beautifier: indents HTML, which the strict XML parser often rejects.
Last updated