EthicalFusion

XML Formatter

Pretty-print and indent XML so it's easy to read and debug.

Was this tool helpful?

Thanks for the feedback!

Paste compact or messy XML and this XML formatter puts every tag on its own line, indented two spaces per nesting level, so the structure of a feed, sitemap, SOAP response or configuration file becomes easy to follow. It works in your browser and formats even markup that is not well-formed, which makes it a reading aid rather than a validator.

What happens to your XML

  1. Whitespace between tags is removed.
  2. The text is split into tags and text runs, and each one goes on its own line.
  3. An opening tag increases the indent for what follows; a closing tag steps back one level.
  4. Self-closing tags such as <empty/>, the <?xml ?> declaration and comments sit on their own lines without changing the depth.

Example

The input <root><item>A</item><item>B</item></root> becomes:

<root>
  <item>
    A
  </item>
  <item>
    B
  </item>
</root>

Notice that text content moves onto its own line. A book record such as <title>Godaan</title> therefore takes three lines, with Godaan indented one level deeper than its tags.

Use the output for reading, keep the original for processing

Because text is placed on separate lines, the formatted file contains extra line breaks and spaces inside elements. Most data parsers that trim text will not care, but whitespace is meaningful in some documents:

  • elements marked xml:space="preserve";
  • mixed content, where text and inline tags interleave, as in XHTML or the document.xml inside a DOCX file;
  • digitally signed XML such as SAML assertions and signed e-invoices, where any whitespace change invalidates the signature.

Read and debug with the formatted copy, but send or store the untouched original.

It formats, it does not validate

Broken markup is still indented, just unevenly. For <a><b>x</a> the closing </a> ends up aligned under <b>, one level too deep, and that misalignment is often the quickest clue to a missing closing tag. Attribute values or CDATA sections that contain angle brackets can also be split in odd places. To confirm a file is well-formed, open it in Chrome or Firefox, which report the line and column of the first parsing error.

Where it helps

Common jobs include checking a sitemap.xml before submitting it to search engines (the XML Sitemap Generator builds one from scratch), reading an RSS or Atom feed, inspecting a SOAP API response, reviewing Android layout files and looking through accounting data exported as XML from software such as Tally. When a JavaScript application needs the same data, the XML to JSON Converter does the conversion.

Frequently asked questions

Why is the text inside my tags on a separate line?
The formatter gives every tag and every piece of text its own line. That makes deep structures easy to scan, at the cost of adding whitespace around element text.
Does it check whether my XML is valid?
No. It indents whatever it receives, including broken markup. A closing tag that does not line up with its opening tag is a hint that something is missing.
Can I use the formatted XML in production?
Keep the original for anything that will be parsed, signed or submitted. The extra whitespace can change text values and will break digital signatures.
How many spaces does it indent by?
Two spaces per nesting level. There is no option for tabs or a different width.
Can I format HTML with this tool?
It will run, but HTML has void elements such as br and img that never close, which throws the indentation off. The HTML Formatter handles those correctly.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.