DevTools Hub

XML Formatter

Format and inspect XML documents.

Runs locally
InputXML
147 chars
OutputFormatted XML
Valid XML
Fix the XML error to generate output.
Errors
No errors found.

How it works

Formatting re-indents an XML document with two spaces per level, putting each child element on its own line while keeping short text-only elements such as <name>value</name> on a single line so the result stays compact.

Parsing uses the browser's own DOMParser in XML mode, so the document has to be well formed. When it is not, the parser's error message is shown, which usually names the line and the construct that broke, such as a tag that was never closed or a stray ampersand.

Comments and CDATA sections are kept as they were, empty elements are collapsed to the self-closing form, and an XML declaration is re-emitted only when the input started with one. Minify does the same round trip without the indentation.

Frequently asked questions

What makes an XML document well formed?
Every element is closed, tags nest without overlapping, attribute values are quoted, there is exactly one root element, and the characters & and < appear only as the entities &amp; and &lt;.
Does it validate against a DTD or an XSD schema?
No. It checks that the document is well formed, which is the syntax layer. Validating against a DTD or an XML Schema requires fetching and applying that schema, and this tool makes no network requests.
Are comments and CDATA sections preserved?
Yes. Comments are re-emitted in place, and the contents of a CDATA section are passed through untouched, so escaped markup or script inside a CDATA block survives formatting intact.
Why did my XML declaration disappear?
The declaration is only written back when the input begins with one. Add <?xml version="1.0" encoding="UTF-8"?> at the top of the input and the formatter will keep it in the output.
Can it format an HTML document?
Only if the HTML happens to be well-formed XML, as XHTML is. Ordinary HTML with unclosed tags such as <br> or <li> will be rejected by the XML parser. Use the HTML encoder for HTML entities instead.