XML Tools
XML Formatter
Pretty-print or compact an XML document, with mixed content and CDATA preserved exactly.
Understand the format
How XML Formatter works
Formatting XML is riskier than formatting JSON, because whitespace inside an element can be part of the data rather than decoration.
Whitespace in XML is not always insignificant
In JSON, whitespace between tokens carries no meaning, so a formatter can reindent freely. XML has no such guarantee. Text inside an element is content, and unless a schema declares an element to hold element-only content, a parser must hand every space, tab, and newline to the application. That is why the XML specification calls the whitespace between elements "ignorable" only when a DTD or schema says it is.
This formatter therefore treats the two cases differently. An element whose children are only elements can be reindented safely, because the whitespace between those children is separator, not content. An element containing text alongside elements is mixed content, and its inner text is reproduced exactly, spaces included.
Why mixed content is the case that goes wrong
Take <p>Hello <b>world</b> again</p>. A naive formatter trims each text node and puts every child on its own line, which turns the sentence into "Helloworldagain" when the document is later read back. The words have been silently joined. Anyone formatting documentation, XHTML fragments, or DocBook has met this bug.
The rule that avoids it is simple: if an element contains any non-whitespace text, do not touch the inside of that element. This page follows that rule, which is why some elements stay on one line even when the rest of the document is indented.
What CDATA and comments are for
A CDATA section holds text the parser must not interpret, which is how a document embeds a fragment containing < and & without escaping every character. The content is data, so it is reproduced byte for byte here, and it is never reindented.
Comments survive formatting but are removed by minifying, which is the usual trade-off: indentation is for people, and the minified form is for the wire, where a comment is payload nobody reads.
Step by step
How to use XML Formatter
- Paste the document into the input box.
- Press Indent for a readable two-space layout, or Minify to collapse it to a single line.
- If a red banner appears, fix the reported syntax error first; a document that does not parse cannot be reprinted.
- Copy the result, or open the same document in the XML Validator to see element and attribute counts.
The document is parsed and reprinted by a parser written for this site and running in your browser. Nothing is uploaded, logged, or stored.
Troubleshooting
Common mistakes and how to fix them
- Reformatting a document that carries an XML digital signature.
- The signature covers a canonical form of the bytes. Reindenting breaks it. Sign after formatting, never the other way round.
- Assuming indentation is always safe because it is safe in JSON.
- XML text nodes are data. Only element-only content can be reindented without changing what the parser reports.
- Minifying a document that relies on comments as instructions.
- Some build and config pipelines read directives from comments. Minifying removes them.