FreeToGenerate.com

100% free · no sign-up · everything in your browser

0

Everything runs in your browser. Nothing is uploaded.

Also available in: Español · Português · Français · العربية

XML Formatter: Format, Indent, and Minify XML

Paste XML on one side and get clean, readable markup on the other — indented the way you choose, minified when you would rather, and never rewritten where it actually holds data.

What is an XML formatter?

An XML formatter takes an XML document and rearranges only its layout — the indentation and line breaks between tags — so its structure is readable at a glance, without touching the data the document actually carries. Paste in a wall of unindented markup and it comes back with each element nested under its parent, spaces or a tab marking each level, so you can see at a glance where one element ends and the next begins.

You reach for one whenever XML shows up somewhere it was never meant to be read by a person: a machine-generated feed that arrives as one continuous line, a configuration file a build tool wrote without any regard for whitespace, an API response you are debugging in a browser tab, or a sitemap you want to check by eye before submitting it. In every one of those cases the content is already correct — what is missing is a shape a human can follow.

This one runs entirely in your browser: paste XML into the left pane and the formatted version appears on the right as you type, with a copy button next to it and a choice of 2 spaces, 4 spaces, or a tab for the indent. A Minify instead checkbox does the opposite, collapsing the whole document onto a single line.

How to format XML

  1. Paste your XML. Paste or type your document into the left-hand pane, and the formatted result appears on the right immediately, updating as you keep typing.
  2. Choose an indent. Pick 2 spaces, 4 spaces, or a tab, or tick Minify instead if you want the document collapsed onto one line rather than indented.
  3. Copy the result. Use the copy button above the right-hand pane to take the formatted XML, and check the panel underneath it for anything the tool deliberately left untouched.

Why this formatter only changes layout, never data

XML and HTML look alike, but they disagree about whitespace. The XML 1.0 specification is explicit about it: an XML processor must always pass all characters in a document that are not markup through to the application. A run of spaces between two tags is not decoration waiting to be tidied up — it is character data, part of the content the document is transmitting. A formatter that reindents everything without asking is quietly rewriting your data, even though what changed on screen looks purely cosmetic.

This tool draws its line accordingly: it changes exactly one kind of thing. Where a piece of text sitting between two tags consists of nothing but whitespace — padding that only ever existed to make the markup readable, and that carries no meaning of its own — it gets replaced with the new indentation you asked for. Every other character comes out exactly as it went in: the same text, the same attribute values, the same punctuation, in the same order.

That is not a promise made lightly. It is enforced by a test that concatenates every piece of the document that is not whitespace-only, before formatting and after, and requires the two strings to match byte for byte. The test runs over a hand-written corpus and a 2,000-document generated sweep, including a control case that proves the comparison is actually capable of failing rather than trivially passing every time. Across all of it, 335 assertions have to hold.

What it refuses to touch, and why

Two kinds of element are left exactly as found, and the panel below the panes reports how many of each it found rather than silently reindenting them anyway. The first is anything carrying xml:space="preserve". The specification defines the attribute for exactly this purpose: it may be attached to an element to signal an intention that in that element, white space should be preserved by applications, and the value preserve indicates the intent that applications preserve all the white space. It is inherited, too — every element inside one marked this way preserves whitespace as well, until an xml:space="default" further down switches it back off, and this formatter honours both directions.

The second is any element that holds text of its own, whether that text stands alone — a title element wrapping a book title — or sits alongside child elements, as in a paragraph with a bold word in the middle of it. In both cases the text is content, and reindenting around it would insert whitespace directly into it.

That second rule used to be narrower, and a test is why it is not anymore. It originally excluded only mixed content — text and child elements together — and happily reindented an element whose only child was plain text. Doing that turns a simple element wrapping the word text into one where that word now has a newline and four spaces in front of it and a newline and two spaces after it: five extra characters, and they are content, not layout. The invariant test comparing before and after failed on the very first document in its own corpus, which is exactly how the rule got widened to cover this case too.

Several other things pass through untouched without needing any special-case logic at all, because the formatter never looks inside them: comments (the specification requires that the string -- never occur within one), CDATA sections (where, per the specification, only the CDEnd string is recognized as markup), processing instructions, and a document type declaration — even one with an internal subset in square brackets whose contents can themselves contain a greater-than sign. Attribute values are left alone too, including ones containing a greater-than sign or the other kind of quote mark.

Why is it free?

Everything above happens on your machine. The XML you paste in is parsed, checked, and reformatted by code running in your own browser tab — it is never sent anywhere, which is also why the formatted result appears instantly as you type instead of after a round trip to a server.

There is no account to create, no watermark stamped into the output, and no limit tied to a plan. A tool like this costs nothing to run because it is not running on a server at all, and that is reason enough to keep it free.