XML Tools

XML Formatter

Pretty-print or compact an XML document, with mixed content and CDATA preserved exactly.

Start here Paste the document into the input box.

<?xml version="1.0" encoding="UTF-8"?>
<order id="1042" placed="2026-08-15">
  <customer>
    <name>Ada Lovelace</name>
    <email>ada@example.com</email>
  </customer>
  <items>
    <item sku="A-1" qty="2">Analytical engine</item>
    <item sku="B-7" qty="1">Punch card set</item>
  </items>
  <note><![CDATA[Deliver before 5pm & ring the bell]]></note>
</order>

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

  1. Paste the document into the input box.
  2. Press Indent for a readable two-space layout, or Minify to collapse it to a single line.
  3. If a red banner appears, fix the reported syntax error first; a document that does not parse cannot be reprinted.
  4. 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.

Worked examples

XML Formatter examples explained

Element-only content is reindented

Input

<order><item sku="A-1">Widget</item><item sku="B-7">Gasket</item></order>

Result

<order>
  <item sku="A-1">Widget</item>
  <item sku="B-7">Gasket</item>
</order>

The whitespace added between the two items is separator, not content, so nothing about the data changes.

Mixed content is left alone

Input

<p>Hello <b>world</b> again</p>

Result

<p>Hello <b>world</b> again</p>

The spaces either side of <b> are part of the sentence. Reindenting this element would delete them and join the words.

Reference

What formatting changes and what it preserves

What formatting changes and what it preserves
Part of the documentIndentMinify
Whitespace between elementsRewritten as indentationRemoved
Text inside mixed contentPreserved exactlyPreserved exactly
CDATA sectionsPreserved exactlyPreserved exactly
CommentsKeptRemoved
Attribute order and valuesUnchangedUnchanged
XML declaration and DOCTYPEKept on their own lineKept

Practical Guide

How teams use XML Formatter

Common use cases

  • Make a machine-generated SOAP envelope or config file readable before reviewing it.
  • Collapse a document to one line for a log entry, a shell argument, or an HTTP body.
  • Normalise indentation before committing a fixture so version-control diffs stay small.

Checks before trusting the result

  • Formatting cannot repair broken syntax; validate the document first if it will not parse.
  • Do not reformat a signed document: an XML signature covers the bytes, and reindenting invalidates it.
  • Check that mixed-content elements still read correctly after formatting.

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.

FAQ

XML Formatter questions, answered

Does formatting change my data?

Not for element-only content, where whitespace is a separator. Text inside mixed content and CDATA is reproduced exactly, so the parsed result is the same document either way.

Why is one element still on a single line?

Because it contains text as well as elements. Breaking it across lines would add or remove spaces inside the sentence, which changes the content.

Are comments removed?

Indenting keeps them. Minifying removes them, along with the whitespace between elements.

Can it format HTML?

Only if the HTML happens to be well-formed XML. Real-world HTML allows unclosed tags such as <br> and <li>, which XML forbids. Use the HTML Renderer to inspect markup instead.

Does the document leave my browser?

No. Parsing and reprinting both happen in the page, so configuration files and message payloads stay on your machine.

Go deeper

Specifications and guides