Account

What new tool do you want?

Developer Tools

XML Formatter & Validator

Pretty-print and validate XML with readable indentation, or minify it — errors point to the exact line and column.

How to use

  1. 1Paste your XML.
  2. 2Pick an indent or Minify and copy the result.

Frequently asked questions

What makes XML invalid?

Common problems are unclosed or mismatched tags, unescaped & or < in text, attributes without quotes and more than one root element.

Are comments and CDATA kept?

Yes. Formatting only changes whitespace between tags; comments, CDATA and processing instructions are preserved.

Does formatting change whitespace inside text?

Leading and trailing whitespace in text-only elements is trimmed. Use Minify=off with care for whitespace-sensitive formats.

A custom tokenizer, not a DOM round-trip

Rather than parsing XML into a DOM tree and re-serializing it (which can silently normalize things like attribute quoting or self-closing syntax), this formatter tokenizes the source with a single regex pass and re-indents based on a depth counter that increments on open tags and decrements on close tags — so `<a><b x="1">hi</b><c/></a>` becomes properly nested output while self-closing tags like `<c/>` correctly never increase depth. One specific behavior worth knowing: when an element contains only text and no child elements (`<tag>text</tag>`), the formatter deliberately keeps it on one line instead of breaking the text onto its own line — this is what keeps formatted XML from turning every leaf value into three lines.

Validation happens before formatting, always

Every path — pretty-print or minify — runs the text through `XMLValidator.validate` first, which means malformed XML (unclosed tags, multiple root elements, unescaped `&` or `<` in text, more than one root) is rejected with a line and column before any formatting is attempted, rather than producing a garbled best-effort result. That's a meaningfully different guarantee than a text-based beautifier would give you: you know the output is at least well-formed XML, not just re-indented garbage.

What's preserved versus what's normalized

Comments, CDATA sections and processing instructions (`<?xml version="1.0"?>`) pass through untouched — only the whitespace between tags is added or removed. Leading/trailing whitespace inside text-only elements is trimmed, which is safe for typical config and data XML but worth turning off (use the Minify toggle with care, or skip formatting) if you're working with a genuinely whitespace-sensitive document like DocBook or an XML-based templating format where significant whitespace is part of the content.

Worked example: XML Formatter & Validator

Feed in the single-line `<?xml version="1.0"?><a><b x="1">hi</b><c/></a>` with 2-space indent and you get: <?xml version="1.0"?> <a> <b x="1">hi</b> <c/> </a> Note `<b x="1">hi</b>` stays on one line since it's a leaf element with only text content, while `<a>` and its children are properly nested. Run `<a><b></a>` (a mismatched tag) through it and you get a validation error instead of a guess at formatting, because the mismatch is caught before any indentation logic runs.

Minify still checks the document before it squeezes the tags

Indent opens on 2 spaces. Tab is another choice. Minify does not rebuild the document. It checks that the XML is valid first, and only then removes whitespace that sits between a closing angle bracket and the next opening one, and trims the ends. Text inside a tag is left as it was. A mismatched tag fails before any of that, with a line and a column and the reason. The download uses an xml name. Pretty formatting uses the indent I picked, including a tab character when that is the choice, rather than a fixed number of spaces.

Related tools

More: Developer Tools