XML Formatter
- 182
- Bytes in
- 216
- Bytes out
- +34
- Change
The formatter indents XML so its structure is visible, or collapses it to a single line. The important detail is what it refuses to do: an element containing only text stays on one line, because adding indentation inside an element changes the text it contains.
How it works
The document is split into tokens (open tags, close tags, self-closing tags, text, comments, CDATA sections and declarations) and reassembled with one token per line, indented by nesting depth.
- An element whose only child is text is written as one line. Splitting <title>Dune</title> across three lines makes the value "\n Dune\n", which is a different string.
- CDATA sections and comments pass through as single tokens, so their contents are never reindented.
- Declarations and processing instructions, the <?xml ... ?> line and anything starting <!. Sit at the top level and do not open a nesting level.
- Minifying removes whitespace between tags and drops comments, leaving text content exactly as it was.
This is a formatter, not a validating parser. When open and close tags do not balance it says so and still produces output, with indentation drifting from the point of the mismatch, which is often the fastest way to see where a tag went missing.
Examples
A one-line catalogue
XML
<catalog><book id="1"><title>Dune</title></book></catalog>
Result
Three indented levels, with <title>Dune</title> on a single line
The title element keeps its text inline. Every other element opens a new level, because their children are elements rather than text.
Minifying
XML
A formatted document with comments
Result
One line, comments removed
Whitespace between tags is insignificant in most XML vocabularies, so removing it is safe. Whitespace inside a text element is not, and is kept.
An unclosed tag
XML
Six open tags and five close tags
Result
Flagged as unbalanced; output still produced
Indentation keeps increasing after the missing close tag, which points straight at where it should have been.
Frequently asked questions
Why is my element kept on one line instead of being indented?
Because its only child is text, and indentation inside an element becomes part of that text. In XML whitespace inside an element is significant unless a schema says otherwise, so <title>Dune</title> split over three lines would hold "\n Dune\n" rather than "Dune".
Does it validate my XML?
No. It tokenises and reindents, and it warns when open and close tags do not balance, but it does not check against a DTD or schema, verify namespaces, or reject malformed attributes. A document can format cleanly and still be invalid.
Is it safe to minify XML?
Usually, because whitespace between tags is insignificant in most vocabularies. It is not universally safe: mixed-content documents, where text and elements sit side by side inside the same parent, can lose meaningful spacing. Check the result if your documents look like prose with markup in it.
What happens to CDATA sections?
They pass through as a single unit with their contents untouched. That is the point of CDATA. It holds text the parser must not interpret, so reindenting inside one would corrupt exactly the content it was protecting.
Why are comments removed when minifying but kept when formatting?
Because minifying is for size and comments are pure overhead in a transmitted document. Formatting is for reading, where comments are often the most useful part. If you need comments preserved through a minify, that is a job for a serialiser rather than a text tool.