Skip to content
AgileLogics

XML Formatter

Parses an XML document with the browser's native XML parser and re-serializes it with consistent 2-space indentation. Invalid XML is flagged with the parser's error message.

How it works

Paste your XML into the input panel and click Format.

What is an XML formatter?

An XML formatter takes XML that's hard to read, usually because it's minified onto one line or inconsistently indented, and re-prints it with one element per line and indentation that shows the nesting. This one parses your input with the browser's native XML parser first, so it also tells you when the document isn't well-formed.

XML (Extensible Markup Language) is defined by the W3C's XML 1.0 specification. It's still common in SOAP APIs, RSS and Atom feeds, sitemaps, Android layouts, Maven pom.xml files and office document formats.

How to format XML

  1. Paste your XML into the input box.
  2. Click Format.
  3. Read or copy the indented result. If there's a syntax error, the error message replaces the output.

Worked example

This one-line catalog:

<?xml version="1.0" encoding="UTF-8"?><catalog><!-- spring list --><book id="bk101" lang="en"><title>XML Basics</title><price currency="USD">29.99</price><instock/></book></catalog>

is formatted as:

<catalog>
  <!-- spring list -->
  <book id="bk101" lang="en">
    <title>XML Basics</title>
    <price currency="USD">29.99</price>
    <instock/>
  </book>
</catalog>

Notice what happened along the way. Elements with only text (<title>, <price>) stay on one line. The empty <instock/> stays self-closing, and an empty pair like <instock></instock> would be written that way too. The comment is kept. The <?xml ...?> declaration at the top is gone.

How it works and what that means

The formatter uses DOMParser with the application/xml type, which is a strict parser: one unclosed tag or stray & and the whole document is rejected, as the XML spec requires. If parsing succeeds, the tool walks the element tree and prints each node with two spaces per level.

That walk only knows about elements, comments and text, which leads to some limits you should know before you save the output anywhere:

Input What the formatter does
<?xml ...?>, <!DOCTYPE ...>, <?pi ...?> before the root Left out of the output
<b>Tom &amp; Jerry</b> Printed as <b>Tom & Jerry</b>, which is invalid XML
note="say &quot;hi&quot;" Printed as note="say "hi"", also invalid
<a><![CDATA[x < y]]></a> CDATA content dropped, printed as <a/>
<p>Hello <b>world</b> again</p> Text beside child elements dropped: only <b>world</b> remains
<a> spaced text </a> Leading and trailing spaces trimmed, inner spaces kept
xmlns:x="urn:x" and <x:item> Kept as written

In short, it's dependable for data-style XML, where every element holds either child elements or a single text value: API responses, config files, feeds and sitemaps without CDATA or escaped characters. For document-style XML with mixed content, or anything containing &amp;, &lt; or CDATA, use the output only as a reading aid and keep your original file.

Well-formed is not the same as valid

The parser checks that your XML is well-formed: tags are closed and properly nested, attributes are quoted, there's a single root element, and special characters are escaped. It doesn't check the document against a schema (XSD) or DTD, so a well-formed file can still be missing a required element. Common well-formedness errors:

Mistake Fix
<a><b></a> Close <b> before </a>
<item id=5> Quote attribute values: id="5"
<name>R&D</name> Escape the ampersand: R&amp;D
Two top-level elements Wrap them in a single root element
Text before <?xml ...?> The declaration must be the very first thing in the file

Related tools

Last updated

Frequently asked questions

What happens if my XML is invalid?
You get an "Invalid XML" message with the browser parser's first error instead of output. In Chrome and Edge it includes the line and column, for example "error on line 1 at column 10: Unexpected closing tag". Fix that spot and format again.
Why did the XML declaration disappear?
The formatter only re-prints the root element and everything inside it. The <?xml ...?> declaration, a DOCTYPE and any processing instructions outside the root are not included in the output, so add them back if your file needs them.
Why is some of my text missing after formatting?
The formatter handles elements that contain either child elements or text, not both. In mixed content such as <p>Hello <b>world</b> again</p>, the loose text around the child element is dropped. CDATA sections are dropped too. Don't use the output for documents like XHTML, DocBook or anything with CDATA.
Does it escape special characters correctly?
No, and this matters if you plan to save the result. Entities are decoded when parsing but not re-escaped on output, so Tom &amp; Jerry is printed as Tom & Jerry, which is no longer valid XML. Use the output for reading, or re-escape &, < and quotes in attribute values before reusing it.
Are namespaces and comments kept?
Yes. Namespace declarations are printed as ordinary attributes, prefixed element names like x:item are kept, and comments inside the root element stay in place on their own lines.