XML Formatter

Pretty-print XML with 2 spaces, 4 spaces or tabs, or minify it to a single line. Well-formedness is checked as you type, and element count and nesting depth are shown.

Make a wall of XML readable, or pack it back down. Paste XML to reindent it with the spacing you prefer, or minify it to a single line. The document is checked for well-formedness as you type, and the counters tell you how many elements it holds and how deep it nests.

Formatting XML without breaking it

Whitespace in XML is content

This is the one fact that separates XML formatting from JSON formatting. In JSON, whitespace between tokens is meaningless and removing it is always safe. In XML, the characters between <name> and Ada are part of the element's text node. Reindenting a document technically changes what it contains.

In practice this almost never matters, because the overwhelming majority of XML uses element-only content — elements that contain other elements and nothing else — where surrounding whitespace is ignorable by every schema. The formatter only adds indentation inside such elements. When an element holds actual text, or a mix of text and child elements as HTML-like markup does, its content is left exactly as it was found. That single rule keeps <p>Hello <b>there</b></p> from silently gaining spaces around there.

Well-formed is not the same as valid

The check performed here is well-formedness, which is what the XML specification requires of every parser: one root element, every tag closed and correctly nested, attribute values quoted, and & and < escaped inside text. If any of that fails, the document is rejected outright — XML famously has no error recovery, unlike HTML.

Validity is a further step: it asks whether the document matches a DTD, XSD or RelaxNG schema, so that <invoice> really does contain a <total> and the date really is a date. That requires the schema, which is not part of the document being pasted. A green result here means the syntax is sound and any parser will accept the file. It does not mean the receiving system will consider the contents correct.

Minification and self-closing tags

Minifying removes the ignorable whitespace between elements and puts the whole document on one line. Because that whitespace was never content, output that started as element-only markup parses to exactly the same tree.

The other saving comes from empty elements: <summary></summary> and <summary/> are defined by the specification as identical, and the shorter form is used on output. The gain is real but modest — XML pays most of its size in repeated tag names, since every element writes its name twice. If a file needs to be genuinely smaller, gzip is the answer; it handles those repeated names far better than any amount of whitespace removal. Do keep a formatted copy for reading, since minified XML is close to unreadable for humans.

Open-source note: implemented in vanilla JavaScript with no third-party libraries.

FAQ

Is my XML uploaded anywhere?
No. The document is parsed and serialised in your browser using the built-in DOMParser, so nothing is sent to a server.
Does formatting change my document?
Only inside elements that contain nothing but other elements, where whitespace is ignorable. Elements holding text or mixed content are left untouched.
Does this validate against a schema?
No. It checks well-formedness — closed and correctly nested tags, one root, escaped characters. Validating against a DTD or XSD needs the schema itself.
Why is my document rejected?
Usually an unclosed or mismatched tag, an unescaped & or <, an unquoted attribute value, or more than one root element. XML has no error recovery.
What do the element and depth counters mean?
Elements is the total number of element nodes in the document. Depth is the longest chain of nested elements, counting the root as level one.
Why did my empty tags change shape?
<a></a> and <a/> are defined as identical by the XML specification, so minified output uses the shorter self-closing form to save bytes.