Format Bench › XML Formatter

XML Formatter and Validator

Paste XML and press Format. The document is parsed with your browser's own XML parser, so a document that formats here is genuinely well-formed rather than merely re-indented.

Runs locally. What you paste never leaves this page.

Well-formed and valid are different words

XML has two distinct quality bars and they are routinely confused. Well-formed means the syntax is correct: every element is closed, tags nest properly rather than overlapping, attribute values are quoted, and there is exactly one root element. Valid means the document additionally conforms to a schema, such as an XSD or DTD, that specifies which elements may appear where.

This tool checks well-formedness, using your browser's own XML parser. That is the useful check for the everyday case of a configuration file or an API response that will not load, because malformed syntax is what breaks parsers. Schema validation requires the schema itself and is a separate step.

What the formatter preserves

  • Attributes stay on their element in original order, with values re-quoted using double quotes.
  • CDATA sections are preserved verbatim. Their contents are deliberately exempt from XML parsing, so reformatting them would change meaning.
  • Comments are kept.
  • The XML declaration, if present, is retained at the top.
  • Elements with a single text child are kept on one line, which is almost always more readable than splitting three lines for one value.
  • Empty elements are collapsed to self-closing form.

Whitespace is not always insignificant

This is the one thing to be careful about. In XML, whitespace inside an element is part of the content unless a schema says otherwise. Pretty-printing adds newlines and indentation between elements, and for the overwhelming majority of documents, where elements contain either child elements or a simple value, that is harmless.

It is not harmless for mixed content, meaning elements that contain both text and child elements interleaved, as in marked-up prose. Reformatting such a document can alter the rendered spacing. If your document contains mixed content and whitespace matters, use the minify direction or leave it alone.

Common parse failures

  • Unescaped ampersands. A bare & in text must be written &. This is the most frequent XML error by a wide margin, and it appears constantly in URLs embedded in documents.
  • Unescaped angle brackets. < in text must be &lt;.
  • Multiple root elements. A document may contain exactly one. Log files that append records without a wrapper produce this error.
  • Mismatched case. XML tags are case sensitive, so <Item> is not closed by </item>.
  • Undeclared namespace prefixes. Using soap:Body without declaring the prefix is not well-formed.

Questions

Does this validate against an XSD or DTD?

No. It checks well-formedness, meaning the syntax is correct and elements nest properly. Schema validation needs the schema document itself and is a separate process.

Will formatting change my document's meaning?

For ordinary documents, no. The exception is mixed content, where an element contains text and child elements interleaved, because there whitespace is part of the content and indentation can alter rendering.

Why does an ampersand break my XML?

The ampersand starts an entity reference in XML, so a bare & is a syntax error. It must be written as &amp;. This is the most common cause of XML parse failures, especially in embedded URLs.

Is CDATA preserved?

Yes, verbatim. CDATA content is exempt from XML parsing by design, so it is passed through untouched rather than re-indented.

Other tools