XML Formatter

Format and validate XML with proper indentation. Minify XML for production use.

Before touching the layout, this tool validates your document with the browser's real XML parser (DOMParser in application/xml mode) and shows the first two lines of the parser error if it fails. Only well-formed XML gets reformatted — a two-space indent per nesting level, one tag per line wherever tags are adjacent.

Validation is a real parse, not tag counting

Because a genuine XML parser runs first, this catches the errors regex-based pretty-printers miss: mismatched case (<Item> closed by </item> — XML is case-sensitive), a second root element after the first one closes, and attribute values missing quotes. The one that surprises people most is entities: XML predefines exactly five (&amp;, &lt;, &gt;, &apos;, &quot;), so &nbsp; — perfectly legal in HTML — is an undeclared-entity error here unless your document declares it in a DTD.

That same strictness means this is the wrong tool for ordinary HTML. Void elements like <br> and <img> without self-closing slashes are well-formed HTML but fatal XML errors. It works well on XHTML, SVG, RSS feeds, Maven POMs, and Android layout files — anything that is actually XML.

How the indenter treats mixed content and minify treats whitespace

The reformatter only breaks lines between adjacent tags (>< with nothing but whitespace between). An element mixing text and inline tags — <p>Hello <b>world</b> again</p> — stays on one line untouched, which is correct: splitting it would inject whitespace into your text content. Declarations, comments, and self-closing tags never increase depth.

Minify is more aggressive than Format: it removes whitespace between tags and also collapses any run of two or more whitespace characters to a single space, including inside text nodes. If your document carries whitespace-significant content — xml:space="preserve" is not honored — minify will alter it, so keep such documents to Format only.

Format a single-line document
Input: <?xml version="1.0"?><catalog><book id="bk101"><title>XML Guide</title><price>29.95</price></book></catalog>
Output: <?xml version="1.0"?> <catalog> <book id="bk101"> <title>XML Guide</title> <price>29.95</price> </book> </catalog>
The <?xml?> declaration stays at depth zero; attributes are left in their original order.
Good to know: If a document fails with an entity error, the fastest fix is usually numeric character references: replace &nbsp; with &#160;, which is valid in any XML document without a DTD.

For ordinary HTML — the documents this validator rejects — the code minifier's HTML mode is the right tool, and the JSON formatter applies the same validate-first discipline to JSON payloads.