DevKitHub

JSON & Data

XML Formatter and Beautifier

Paste XML to indent it, or minify it to a single line. Only whitespace between elements changes — comments, CDATA and anything inside a tag are left exactly as they were.

1 line
5 lines

This tool runs entirely in your browser. Your input is never uploaded, stored or logged.

How it works

The document is tokenised before anything is indented, which is the difference between a formatter and a search-and-replace. A comment, a CDATA section, a DOCTYPE and a processing instruction are each one token, and their contents are never examined — so a "<" inside CDATA is data, not the start of an element, and a formatter built on regular expressions reindents it and quietly corrupts the payload.

Finding the end of a tag is the other place this goes wrong. Searching forward for the next ">" splits a tag whose attribute value legally contains one, such as title="x > y", and everything after it is misparsed. The scan here tracks quoting, so the closing bracket is the one that actually closes the tag. What is inside a tag is then re-emitted verbatim rather than tidied: normalising whitespace between attributes means parsing attribute values, and an attribute value can contain exactly the characters a naive pass would split on.

An element holding nothing but text stays on one line, because a document of leaf values split three ways per element is harder to read than the input was. Structure is checked as it goes and reported rather than thrown: a mismatched closing tag, an element that is never closed and a stray close are each named, and you still get the formatted output, because a half-written document is exactly what somebody is trying to read when they reach for a formatter.

Common problems

Every example below is run against this tool in our test suite, so what it says here is what the tool actually does.

A comment was opened with <!-- and never closed.

<a><!-- todo</a>
Why:
XML comments do not nest and are not ended by a newline, so an unclosed one swallows the rest of the document — including the closing tags that would have made it well formed.
Fix:
Close it with -->. Note that a comment also cannot contain a double hyphen anywhere inside it.

A tag was opened with < and never closed with >.

<root><item
Why:
The document ends in the middle of a tag. Usually it was truncated in transit, or copied from a viewer that cut it off at a column limit.
Fix:
Check whether the source is complete — a truncated XML file is a transport problem, not a formatting one.

Another formatter mangled the contents of a CDATA block.

Why:
Formatters that work on raw text cannot tell a "<" inside CDATA from one that opens an element, so they reindent the payload and change the data.
Fix:
Nothing to do here — CDATA is a single token and is never touched. Test any other formatter in your pipeline with a CDATA block containing a tag.

Frequently asked questions

Can it change my document?
Only the whitespace between elements. A test asserts that the token stream coming out matches the one going in for every sample document, and that formatting twice changes nothing further.
Does it validate against a schema?
No. It checks that elements open and close in the right order and names any that do not, but it does not know about your DTD or XSD. It is a formatter, not a validator.
Why are my attributes not realigned?
Because tidying them means parsing attribute values, and a value can contain the quotes, spaces and brackets a rewriting pass would trip on. Indentation is worth having; rewriting the inside of a tag is not worth the class of bug it invites.

Last updated