๐Ÿ“ฐ XML Formatter

Input
Output

Pretty-print XML data with clean indentation, or minify it by stripping unnecessary whitespace and line breaks. Uses the browser's built-in XML parser, so conversions are based on correct, standards-compliant parsing.

How to use

  1. Paste the XML you want to format or minify into the left input box.
  2. Choose "Format" or "Minify" mode, and set the indent width (for format mode).
  3. Press the run button to see the result on the right โ€” use the copy button to copy it to your clipboard.

How the calculation works

This tool pretty-prints XML with line breaks and indentation, or minifies it by removing unnecessary whitespace. The XML is first parsed with the browser's DOMParser; if it is not well-formed (an unclosed tag, for example), an error is shown. When formatting, elements are indented by nesting depth, empty elements are written as <tag />, and elements containing only text stay on one line. Comments, CDATA sections and processing instructions are kept. Text and attribute values are escaped again on output ("&" as &amp;, "<" as &lt; and so on), so the formatted result is still valid XML. Indentation can be 2 or 4 spaces.

Worked example

Input <root><item id="1"><name>Heron</name><active>true</active></item></root> Formatted (indent 2) <root> <item id="1"> <name>Heron</name> <active>true</active> </item> </root> <name>Tom &amp; Jerry</name> is still output as Tom &amp; Jerry.

Things to be aware of

  • The XML declaration (<?xml version="1.0"?>) is not included in the formatted output. Add it back at the top if you need it.
  • In mixed content (text with tags inside), whitespace around text is tidied, which can change visible spacing.
  • Your XML is processed in the browser and never sent anywhere.

FAQ

What happens if I enter invalid XML?

It's treated as a syntax error and an error message is shown. Check for unclosed tags or missing quotes around attribute values.

Does this handle XML namespaces?

Yes. Since it parses using the browser's built-in XML parser, XML containing xmlns attributes is handled correctly.

How are self-closing tags handled?

Elements with no children are output in the <tag /> self-closing form when formatting.