XML Formatter

Format and beautify XML in your browser — well-formedness checked first with exact error locations, dedicated XML rules (not HTML rules), nothing uploaded. Free, no sign-up — runs locally in your browser.

Your work is auto-saved in this browser only, so a refresh won't lose it. Nothing is uploaded.

…or drop a file onto the box above. Files are read locally, never uploaded.

What is an XML formatter?

XML that comes out of a web service, a config generator or a log file is written for machines: whole documents on a single line, nesting invisible, and a structural error — one mismatched tag — enough to make every downstream parser reject the lot. An XML formatter (also called a beautifier or pretty-printer) rewrites the document with consistent indentation: every nested element steps in by one level, siblings line up, and the tree becomes something you can read at a glance and diff without tears.

This one does a second job before it formats, the same discipline as this site's JSON and HTML tools: it checks well-formedness first. XML is unforgiving by design, so if the structure does not check out, you get the line and column of the first problem instead of a confidently reformatted mess.

How to use this tool

  1. Paste your XML into the box above (or press Load sample to see a worked example). You can also upload an .xml file or drop one onto the box — the file is read in your browser and never sent anywhere.
  2. Choose an indent — 2 spaces is the common default; 4 spaces and tabs are one click away.
  3. Press Format. If the document is well-formed you get the reformatted result and a line count; if it is not, you get the precise location of the first structural problem — fix that one and format again.
  4. Tick Auto-format as I type if you would rather not press the button: the result updates as you edit (very large inputs are still formatted on demand, to keep typing smooth). While the document is mid-edit and not yet well-formed, the message line tells you so and your last good result stays on screen.
  5. Use Copy result or Download result to take the formatted document with you. Your input above is never modified — the result is a separate copy.

Built for XML, not borrowed from HTML

This formatter has its own XML tokenizer — the document is never piped through the HTML beautifier, because HTML's rules are wrong for XML. The checker enforces XML's actual contract: every start tag must be closed by its matching end tag, tag names are case-sensitive (</A> does not close <a>), there is exactly one root element, and text lives inside it. Comments, CDATA sections, processing instructions and a doctype (including one with an internal subset) are recognised as the constructs they are: a </tag> inside CDATA is data, not markup, and is never "balanced" against anything.

What formatting never changes

Formatting here is a layout rewrite, never a data rewrite. Tag names keep their spelling and case. Attributes are never reordered, added, removed or split across lines, and their values pass through byte-for-byte — entities included, never decoded. An element whose only content is text stays on one line, its text trimmed of the surrounding layout whitespace only. Mixed content — text and elements side by side — puts each text chunk on its own line, characters unaltered. The single respelling, stated openly: an element with no content at all, written <empty></empty>, is printed as <empty/> — identical to every XML parser, and the form every XML author writes by hand.

Examples

This compressed document:

<order id="1042"><customer>Ada</customer><items><item sku="A-1">2</item><item sku="B-7">1</item></items><note><![CDATA[Leave at <front> desk]]></note></order>

formats into:

<order id="1042">
  <customer>Ada</customer>
  <items>
    <item sku="A-1">2</item>
    <item sku="B-7">1</item>
  </items>
  <note><![CDATA[Leave at <front> desk]]></note>
</order>

And this broken fragment — <name> closed by </Name> — is reported as the case-sensitive mismatch it is, instead of being reformatted into a guess:

<user><name>Ada</Name></user>

Common XML mistakes this catches

Frequently asked questions

Is my XML uploaded anywhere?

No. Checking and formatting happen in your browser, on this page. Nothing is sent to a server — you can disconnect from the network and the tool keeps working. Your draft is auto-saved in this browser only, for 7 days, and the Clear saved draft button deletes it immediately.

Why did the tool refuse to format my document?

Because the well-formedness check that runs first found a structural problem: a mismatched end tag, an element never closed, text outside the root element, or more than one root element. The error message gives the line and column of the problem and, for a mismatch, where the element it expected was opened. XML parsers are strict by design — the document is either well-formed or it is not XML — so instead of guessing, the tool shows you the first problem; everything after it is usually a cascade.

What exactly does formatting change?

Only layout whitespace between elements: indentation and line breaks. An element holding only text stays on one line with its text trimmed of surrounding layout whitespace (the text itself is never altered internally); an element with child elements breaks onto multiple lines, one child per line. Attributes stay on the tag's line, in their original order and spelling, and attribute values pass through byte-for-byte. Comments, CDATA sections and processing instructions keep their content exactly.

Why did <empty></empty> come back as <empty/>?

That is the one respelling the formatter performs — and the two forms are the same document to every XML parser that exists: an element with no content. It is stated here and on this page rather than done quietly. Nothing else is ever respelled: tag names keep their case (and must — XML names are case-sensitive), entities are not decoded, and namespaces are never touched.

Is this the same as the HTML Formatter?

No, and the difference matters. HTML allows omitted end tags, void elements and case-insensitive names; XML allows none of those. Piping XML through an HTML formatter would apply the wrong rules and can mangle a document while calling it fixed. This tool has its own XML tokenizer and checker: <li> without </li> is an error here, </A> does not close <a>, and a second root element is rejected.

Does it validate against a schema (XSD/DTD)?

No. It checks well-formedness — the structural rules every XML document must obey — not validity against a particular schema or DTD. A document can be perfectly well-formed and still wrong for your application; only your schema can judge that. A doctype with an internal subset is preserved as-is, not interpreted.

Can I open an .xml file instead of pasting?

Yes — use the Upload button or drag a file onto the input box. The file is read locally by your browser (it is never uploaded to a server), its contents land in the input box, and formatting runs immediately. Download result does the same job in the other direction.

Related tools