XML Formatter & Beautifier

This XML formatter beautifies cramped, single-line XML into neatly indented markup, or minifies it back for transport. It also works as an XML format checker: if a tag is unclosed, an attribute is unquoted or an ampersand is not escaped, you see the exact line and column instead of broken output.

Output
Indent
Well-formed XML

Runs entirely in your browser. Nothing you paste is uploaded or stored.

How do you format XML?

Paste the XML and the formatter splits it into tags, text, comments and CDATA sections, then prints each element on its own line, indented by how deeply it is nested. Elements that contain only text stay on one line, so <title>Rain</title> is not spread over three. If the XML is not well-formed, the first error is shown with its position instead.

How to use the XML formatter online

  1. Paste XML, drop an .xml, .svg or other XML file on the input, or click Open file. Files are read locally.
  2. Choose Beautify and an indent of 2 spaces, 4 spaces or tabs, or Minify to remove the whitespace between tags.
  3. Tick Remove comments to strip <!-- … --> blocks from the output.
  4. Copy or download the result.

What “well-formed” means

The W3C XML 1.0 specification calls a document well-formed when it follows the basic syntax rules. A parser must stop at the first violation, so a formatter cannot safely guess what broken XML meant. The rules this XML validator and formatter checks are:

  • There is exactly one root element that contains everything else.
  • Every start tag has a matching end tag, or is self-closing (<br/>), and tags close in the reverse order they opened.
  • Tag names are case-sensitive: <Item> is not closed by </item>.
  • Attribute values are in single or double quotes, and an element cannot repeat an attribute.
  • & and < in text must be written as &amp; and &lt;. Only five named entities are predefined; others such as &nbsp; need a DTD.
  • The XML declaration, if present, comes first.

After this check passes, the XML is also run through your browser's built-in XML parser, which catches rarer problems such as invalid characters, and any message it reports is shown.

XML beautify rules and whitespace

Pretty printing changes whitespace, and in XML whitespace between tags is usually, but not always, insignificant. This formatter drops text nodes that contain only spaces and line breaks, trims the text inside elements, and keeps comments, CDATA sections, processing instructions and the DOCTYPE exactly as written. Attribute values and their quote style are preserved; only the spacing between attributes is normalised.

<note><to>Tove</to><from>Jani</from><body>Don't forget me!</body></note>

<note>
  <to>Tove</to>
  <from>Jani</from>
  <body>Don't forget me!</body>
</note>

If your document relies on exact whitespace, for example mixed content in a document format or elements marked xml:space="preserve", compare the output before using it. The text compare tool shows every difference. For JSON data, use the JSON formatter instead.

Well-formed vs. valid XML

“Valid” has a stricter meaning in XML than in everyday speech. A valid document is well-formed and matches a schema (a DTD, XSD or RELAX NG grammar) that says which elements may appear where. This free online XML formatter checks well-formedness only, which is what formatting needs. Schema validation requires the schema file and is usually done in your build or with a tool like xmllint --schema schema.xsd file.xml.

Frequently asked questions

How do I check if XML is valid?

Paste the XML into the formatter above: if it is well-formed you see "Well-formed XML" and the formatted output, otherwise the first error with its line and column. Note that well-formed only means the syntax is correct; to check that elements and attributes match a particular structure you also need to validate against a DTD or XML Schema (XSD).

Is this XML formatter free?

Yes, this XML formatter is completely free, with no sign-up and no limits beyond a 20 MB file size. It runs in your browser, so the XML you format is never uploaded to a server.

How to compare two XML files?

To compare two XML files, format both here with the same indent first, so differences in whitespace and line breaks do not hide the real changes, then paste the two results into a text compare tool. The line-by-line diff then shows exactly which elements and attributes were added, removed or changed.