Skip to content
ToolkitHub

XML Formatter

Format and beautify XML

Guide

Pretty-printing XML documents

By Logappradeep · ServiceNow developer

XML still powers a lot of software: SOAP APIs, RSS and Atom feeds, sitemaps, Android layouts, Maven pom.xml files, SVG images, Office documents and countless configuration formats. When an API returns XML as one compact line, reading it means counting angle brackets. Pretty-printing adds line breaks and indentation so the element tree is visible.

How the formatter works

The tool inserts a line break between every pair of adjacent tags, then walks the lines while tracking depth. An opening tag such as <book id="bk101"> increases the indent by two spaces; a closing tag decreases it; and a line that opens and closes on the same line, such as <price>44.95</price>, stays at the current level. Self-closing tags like <item/> and the <?xml ... ?> declaration do not change the depth. For example:

<catalog><book><title>XML Guide</title></book></catalog>

is printed with <book> indented under <catalog> and <title> indented under <book>. Minify removes whitespace between tags to produce the compact form again.

Where it is useful

  • Inspecting SOAP responses and error envelopes while debugging an integration.
  • Checking the structure of an RSS feed or an XML sitemap before submitting it.
  • Reviewing SVG source to find a path or group you want to edit.
  • Shrinking an XML payload before embedding it in a test fixture.

Well-formed versus valid

Two terms often get mixed up. A well-formed XML document follows the basic syntax rules: one root element, every tag closed, tags properly nested, attribute values quoted, and special characters escaped as &lt;, &amp; and so on. A valid document is well-formed and also conforms to a schema (XSD or DTD) that defines which elements are allowed. This formatter does neither check: it re-indents what you give it. If the indentation looks wrong, that often points to an unclosed or mismatched tag.

Pitfalls

Whitespace can be significant in XML. In mixed content, where text and elements are interleaved, such as <p>Hello <b>world</b></p>, adding line breaks changes the text nodes a parser sees. Elements marked with xml:space="preserve" and the contents of <![CDATA[ ... ]]> sections should be left alone. Namespace prefixes such as soap:Envelope are kept as-is, which is correct; never strip them, because the prefix is bound to a namespace URI that consumers rely on.

Formatting runs locally in your browser, so internal API responses are not uploaded. The last input is saved in local storage for your next visit.

Frequently asked questions

Does the XML formatter validate against a schema?

No. It only re-indents the document. Use an XSD or DTD validator to check whether a document is valid for a particular schema.

Can formatting change the meaning of my XML?

In mixed content or elements with xml:space="preserve", added whitespace becomes part of the text. Data-oriented XML such as API payloads is generally unaffected.

Can I format SVG files with it?

Yes. SVG is XML, so you can paste the source and pretty-print it to find elements more easily.

Is my XML uploaded?

No. Formatting and minifying happen in your browser.