XML formatter — pretty-print XML and find well-formedness errors

Paste XML and the formatter checks that it is well-formed, then indents every element, or tells you the line and column where the markup breaks.

Live demo · real result from this tool
Indent
0 keeps attributes on one line.

Formatted XML

<?xml version="1.0" encoding="UTF-8"?><catalog>  <book id="bk101" lang="en">    <author>Gambardella, Matthew</author>    <title>XML Developer's Guide</title>    <price currency="USD">44.95</price>  </book>  <book id="bk102">    <author>Ralls, Kim</author>    <title>Midnight Rain</title>    <price currency="USD">5.95</price>  </book></catalog> 

Processed on your device — nothing is uploaded.

How to use the XML formatter

  1. Paste your XML into the Input box, or edit the sample.
  2. Choose an Indent: 2 spaces, 4 spaces or a tab.
  3. To put attributes on separate lines, set Put attributes on their own lines when an element has more than to a number such as 2. Leave it at 0 to keep attributes on one line.
  4. Untick Keep comments to drop <!-- … --> comments.
  5. Read the Formatted XML or the error message, then copy or download formatted.xml.

What it does and when to use it

XML arrives squashed onto a single line from APIs, SOAP services, RSS and sitemap files, Android layouts, Office documents and many config systems. The formatter turns it into an indented tree, so you can see which element sits inside which and spot missing or extra tags.

It doubles as a quick well-formedness checker. If the markup is broken, you get a message such as “Expected closing tag ‘b’ … instead of closing tag ‘a’” with a line and column, instead of a silently mangled result.

Once your XML is readable, the XML to JSON converter can turn it into JSON, and the XML minifier packs it back down for sending. For SQL inside the same project, use the SQL formatter.

How it works

First, the well-formedness check runs with the fast-xml-parser validator, loaded in your browser when needed. If it fails, you see its message and position. If it passes, the tool’s own XML tokenizer reads the elements, attributes, comments, CDATA sections and processing instructions and prints them with one level of indent per nesting level. An element that holds only text stays on one line, such as <b>1</b>. Mixed content and xml:space="preserve" blocks are copied exactly. Attribute values and quotes are not changed.

Worked examples

These outputs come from the formatter.

Nested elements. <a><b>1</b><c x="1"/></a> becomes:

<a>
  <b>1</b>
  <c x="1"/>
</a>

A broken document. <a><b>1</a> gives: “XML is not well-formed at line 1, column 8: Expected closing tag ‘b’ (opened in line 1, col 4) instead of closing tag ‘a’.”

Attributes on their own lines. With the attribute limit set to 2, <book id="7" lang="en" format="pdf"><title>Notes</title></book> has three attributes, so each goes on its own line:

<book
  id="7"
  lang="en"
  format="pdf">
  <title>Notes</title>
</book>

Limits and tips

  • The first error only. Fix it and paste again; later errors appear one at a time.
  • Namespaces are kept as written, but prefixes are not checked against their declarations.
  • Very large files (many megabytes) may make the page slow, because everything runs on your device.
  • Keep a copy of the original if exact whitespace matters, for example in signed XML.

Frequently asked questions

What does "not well-formed" mean?
It means the text breaks XML's basic rules, for example a tag that is never closed, tags closed in the wrong order, an attribute without quotes, or a bare & character. The formatter shows the line and column of the first problem it finds.
Does it check my XML against a schema (XSD or DTD)?
No. It checks well-formedness only. A document can be well-formed and still be invalid for a particular schema, for example if a required element is missing.
Why is some of my text not indented?
Elements that mix text and tags, such as <p>Mixed <b>bold</b> text</p>, are left exactly as written. Adding line breaks inside them would change the text content. Elements with xml:space="preserve" are kept as written too.
Why does &nbsp; cause an error?
XML defines only five named entities, which are &amp;, &lt;, &gt;, &quot; and &apos;. HTML names like &nbsp; are not defined unless a DTD declares them. Use a numeric reference such as &#160; instead.
Is my XML uploaded anywhere?
No. Checking and formatting happen in your browser, which matters when a file contains customer data or API keys.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close