XML Formatter

Paste raw or minified XML and get clean indented markup — validated as structurally well-formed, with attributes, text and comments left untouched, all in your browser.

  • Free
  • No account
  • Runs in your browser
  • Nothing uploaded
XML FormatterNothing uploaded
157 characters

Runs entirely in your browser — your input is never uploaded, logged, or stored.Privacy policy

What is XML Formatter?

XML is a text format for structured data, made of tags that nest inside each other. The same document can be laid out in many ways — but a single-line response from an API or a minified config from a build tool is genuinely hard to read. This tool turns a wall of text back into the indented, tag-per-line layout that XML is famous for, and checks along the way that the document is structurally well-formed — that its tags nest the way XML requires.

Formatting here means two things that are worth keeping separate. First, re-indentation: each element moves to its own line, nested deeper as its tags nest deeper. Second, validation: for markup to be reformatted at all it must first be structurally well-formed — every open tag closed, tags nested correctly, every attribute value in quotes, and exactly one root element. If any of that fails, the tool says so with a clear message rather than guessing at a layout that was never there. The validation is deliberately structural: it checks what indentation depends on, and everything else the layout keeps intact — text, entities, characters — passes through untouched.

It is also important to be precise about what formatting does not touch. XML text content — the actual words inside tags — is data, and it is never changed. The order of attributes on a tag is preserved as written, entity references like & stay the way they were typed, comments are kept, and CDATA sections (blocks of text with no markup at all) pass through byte-for-byte. The tool rewrites only the whitespace that sits between tags and is not part of the content. That distinction is the entire honesty of a formatter: it makes code readable, it does not invent anything.

The scanner walks the XML into tokens — open tags, close tags, self-closing tags, text, comments, CDATA, and processing instructions — and verifies structural well-formedness as it goes: every open tag is matched by a close tag of the same name in the correct nesting order, there is exactly one root element, attribute values are quoted, and no stray text appears outside the root. Then it re-emits the document, and two rules decide the layout.

Inside an element whose content is all markup — child elements, comments — each token starts on its own line at the chosen indentation, indented by its depth. Text that is only whitespace is formatting: it is replaced by that newline-and-indent, which is why the output derives from the indent you pick, not from whatever spacing the source had.

The moment an element contains real text — a word, a number, a CDATA block — that element's interior is emitted byte-for-byte. Its text is data, so nothing inside is re-spaced or moved onto new lines. CDATA stays inside its own brackets, entity references keep their exact form, and an element that mixes text with child elements keeps that exact mix. Attribute order is always preserved, attribute quotes are copied rather than normalized, and names such as xsi: are treated as plain names.

Worked examples

  • Input: <users><user><name>Alice</name><age>30</age></user><user><name>Bob</name><age>25</age></user></users> → <users> <user> <name>Alice</name> <age>30</age> </user> <user> <name>Bob</name> <age>25</age> </user> </users>
  • Input: <users><user><name>Alice</name><age>30</age></user></users> → <users> <user> <name>Alice</name> <age>30</age> </user> </users> — reformatting replaces only whitespace between tags, never text.
  • Input: <a><!-- keep me --><b>x</b></a> → <a> <!-- keep me --> <b>x</b> </a> — comments are kept and placed on their own line.
  • Input: <c><![CDATA[if (a < b) { run(); }]]></c> → <c><![CDATA[if (a < b) { run(); }]]></c> — CDATA text passes through byte-for-byte, < and & inside it are not markup.
  • Input: <item id="1" qty="2">will &amp; grace</item> → <item id="1" qty="2">will &amp; grace</item> — attribute order, quoting and entity references stay exactly as typed.

How to use XML Formatter

  1. Paste or type your XML into the input box.
  2. Choose the indentation you want: 2 spaces or 4 spaces.
  3. Read the formatted result — or the clear 'not valid XML' message if the document is malformed.
  4. Copy the output. Nothing is stored or uploaded; closing the tab is all the cleanup there is.

Common errors

  • An element is closed with the wrong name or is never closed — <b> opened and never closed. Every open tag must have a matching close tag; the tool reports it as not valid XML.
  • There is more than one root element — two top-level tags with no parent. XML requires exactly one root; the fix is wrapping the fragments in a single container element.
  • An attribute value is not quoted — <item id=1/>. Attribute values must sit inside double or single quotes: <item id="1"/>.
  • Text appears outside the root element — words before the first tag or after the last one. XML documents are exactly one element, with only declarations, comments and whitespace around it.
  • The document uses mismatched quotes or a stray < inside an attribute value — a quote that does not close, or markup characters where the XML grammar does not allow them.

FAQ

Will this tool change my XML data?

No — the data is never touched. The tool rewrites only whitespace between tags that is not part of the content: line breaks and indent become your chosen layout. Text inside elements, the order and quoting of attributes, comments, CDATA, and entity references all pass through exactly as written.

What does well-formed mean, exactly?

This tool calls a document well-formed when it passes its structure checks: exactly one root element, every open tag matched by a correctly nested close tag of the same name, attribute values in quotes, and no stray characters outside the root. That is deliberately a structure check only — the wider XML grammar also constrains characters and entity references, but formatting never rearranges those, so they pass through untouched either way. It is likewise not a schema check, since XML has none unless you attach a DTD, XSD, or similar.

Is my XML uploaded anywhere?

No — formatting happens entirely in your browser using a local XML scanner. Nothing you paste is sent to a server.

Can it fix broken XML?

No, and it says so rather than pretending. If the document fails the structure checks the tool reports it; it never invents close tags, attributes, or a root element to force a layout. Repairing malformed markup by guesswork would silently change your data, which is worse than an honest error.

Why does my indentation not change when I pick a different width?

The layout is only rewritable where the whitespace is formatting: between tags and empty of other characters. If the existing text already separates the tags with content, that whitespace is data and is preserved. This is the same rule that keeps your text, attributes and comments intact, and it applies to indentation too.

How is this different from minifying XML?

This tool formats; it never removes whitespace that is content. A minifier approaches the same document from the opposite direction — removing as much whitespace as possible while keeping it parseable — and still must leave inside-element text alone. If you want the compact form, the same input also works with a minifier; a formatter's job is readability.

Does the tool support namespaces, self-closing tags and CDATA?

Yes. Namespace prefixes like xsi: in names pass through as any other name; self-closing tags like <item/> stay self-closing rather than being expanded into open-and-close pairs; and CDATA sections pass through byte-for-byte whether or not the text inside looks like markup.

G

Prefer AllUtil on Google

One click adds AllUtil to your Google preferences. You'll see our tools highlighted with a Preferred badge in Search and AI answers.

2× more likely to clickWorks in AI Overviews

Reviewed by unreviewed

Written by Hamza AK. We research, build and test every tool before it is published.

Editorial policy

Last updated

Rechecked against the sources on this date, not stamped.

Methodology

This tool tokenizes XML with a local scanner and validates structural well-formedness — one root element, balanced and correctly nested tags, quoted attribute values — before re-emitting it with the chosen indentation. It rewrites only whitespace that sits between tags and contains no other characters; text content, attribute order and quoting, comments, CDATA, and entity references are emitted exactly as typed. Formatting runs locally in the browser — nothing is uploaded.