XML Formatter

In short: paste XML, get it back validated and indented — parsed by your browser's own XML engine, so a document that formats here is well-formed everywhere. Whitespace inside text is never touched, because in XML it can be content.

Validation first, formatting second

Most formatters are pretty-printers that hope. This one parses first: the browser's native XML parser checks the document against the well-formedness rules every XML processor enforces, and if it fails, you get the parser's error — the same failure your server would hit — instead of a confidently indented broken document. Only a document that parses is walked and re-emitted with even indentation, with single-text elements kept inline and empty elements self-closed, plus a small census of the result: elements, attributes, maximum depth. A minifier runs the same parse in reverse. The work happens entirely on this page; XML in the wild is too often confidential (invoices, configs, health and finance messages) to be pasted into a stranger's server.

One honest limitation, stated rather than hidden: formatting is a presentation change, and a few documents should never be reformatted — digitally signed XML (the signature covers the exact bytes), documents whose text content is whitespace-sensitive throughout, and files with a document type declaration whose internal subset you care about preserving verbatim. For the everyday run of configuration files, API payloads, sitemaps, feeds and exported data, indentation is purely cosmetic — and turning a one-line export into something a human can diff, review and debug is the whole point. The statistics line earns its keep there too: a document that suddenly doubles in depth or element count between exports has changed structurally, not just cosmetically, and that is usually the first visible symptom of an upstream bug.

Frequently asked questions

What does this formatter actually do to my XML?

Three things, in order. It parses the document with the browser's real XML parser, which enforces well-formedness — matching tags, quoted attributes, one root element, legal characters — and reports the parser's own error if the document fails, rather than guessing. It then walks the parsed tree and re-emits it with consistent indentation (your choice of 2 spaces, 4 spaces or tabs): each element on its own line, except elements holding a single text node, which stay inline the way a human would write them, and empty elements, which are written self-closed. Finally it reports the document's shape — element and attribute counts and maximum depth. A minify mode runs the same parse and emits the document with the insignificant whitespace stripped. Nothing is uploaded: the parser is the one built into your browser.

Why does the formatter refuse to reformat some whitespace?

Because in XML, unlike HTML, whitespace can be content. Inside mixed content — an element holding both text and child elements, like a paragraph with a bold word — the spaces around the children are part of the document's meaning, and a formatter that re-indents them changes the text. The safe rule this tool follows: it re-indents element-only content freely (whitespace between elements that contain only elements is structural), keeps single-text elements inline, and leaves the text itself untouched — it never trims inside a text node beyond collapsing the purely-whitespace nodes between elements. If your document's meaning depends on exact spacing everywhere (verse, preformatted text, signed XML), do not reformat it at all: canonicalisation is a different, stricter operation, and pretty-printing a signed document will break its signature.

What is the difference between well-formed and valid XML?

Well-formed is syntax: tags nest properly, attributes are quoted, there is one root, the characters are legal. Any XML parser — including the one behind this page — can check it, and it is the least a document must be to be XML at all. Valid is semantics against a contract: the document also obeys a schema (DTD, XSD or RELAX NG) that says which elements may appear, in what order, with what types — an invoice whose total element contains text is well-formed but invalid against its schema. This tool checks well-formedness only, because that is what a parser can know without your schema; if the tool says your document is well-formed and your system still rejects it, the argument is about validity, and the schema is where to look. Confusing the two is the XML world's favourite waste of an afternoon.

Why does my XML fail with an error about entities or encoding?

The two classic causes. Entities: a bare ampersand or angle bracket in text is syntax, not content, so a phrase containing a raw ampersand must be rewritten with the ampersand escaped as an entity reference. Only five entities are predefined in XML — for the ampersand, the two angle brackets and the two quote marks; anything else, like the non-breaking-space entity borrowed from HTML, must be defined in a DTD or it is an error, which is why pasting HTML-flavoured content into XML fails so reliably. Encoding: if the bytes are not the UTF-8 or UTF-16 the declaration claims — a document saved as Windows-1252 with a smart quote is the usual suspect — the parser stops at the first illegal byte and names its position. This page parses text pasted as Unicode, so encoding errors mostly arrive pre-fixed; the entity errors it will show you exactly as a server-side parser would.

XML or JSON — which should new systems use?

JSON won the API era on simplicity: it maps directly onto the data structures of programming languages, carries no attributes-versus-elements ambiguity, and is smaller on the wire. XML remains the better tool where documents (not data) are the point — mixed content, publishing formats like DOCX and SVG, configuration with comments that must survive round-trips — and it is immovable in whole industries (finance messaging, government, publishing) whose schemas are decades deep. The pragmatic answer most systems land on: JSON for new data APIs, XML where documents, signatures or an existing schema ecosystem rule. Either way the documents keep coming, which is why a formatter that validates honestly still earns its place — this one sits beside a JSON formatter on this site for exactly that reason.

Related tools

Behaviour and explanations last verified: 24 November 2026. Everything is calculated in your browser — nothing you type is sent anywhere.