XML Minifier

Paste an XML document, feed or config file to collapse it onto one line. Only whitespace that carries no data is removed, and the document is checked for well-formedness first.

Input

Settings

History

Load from URL

Which whitespace is safe to drop

XML has no notion of “insignificant” whitespace in the parser itself: every space between tags is a text node that a program could read. In practice, though, the line breaks and indentation between elements that contain only other elements (element-only content, like <items> holding a list of <item>) are formatting, and every schema-driven consumer ignores them. That is what this minifier removes. The XML printer classifies each element before deciding:

  • Element-only content: whitespace-only text between child elements is dropped.
  • Mixed content, where text and elements sit side by side as in <p>Hello <b>world</b> and friends</p>, is copied exactly, so the spaces around <b> survive and the sentence still reads correctly.
  • Leaf text such as <title> Release notes </title> keeps its leading and trailing spaces, because they are data.
  • Elements marked xml:space="preserve", and everything inside them, are left untouched.
  • CDATA sections are emitted verbatim, including any runs of spaces.

Inside tags, line breaks between attributes collapse to a single space, and single-quoted attribute values are rewritten with double quotes when that needs no escaping.

What else is removed, and what is kept

Comments are removed by default. Turn on Keep comments when minifying when they carry meaning, such as licence headers, generator notes or conditional markers a downstream tool looks for. The XML declaration, DOCTYPE, processing instructions and namespace declarations are always kept.

Entity references are written back as they appear. &co; stays &co; even when the internal DTD defines it; the built-in XML tokenizer never expands DTD entities, which also protects you from “billion laughs” style expansion attacks. The output means the same thing to a validating parser as the input did.

Two other options apply in minify mode. Empty elements set to Self-close <a/> shortens every <item></item> to <item/>, a few bytes each that add up in large exports. Collapse whitespace-only elements (on by default) empties elements that contain nothing but whitespace, again except where xml:space="preserve" applies or inside <text> and <xsl:text>, where a space is real output. The toolbar’s Sort keys sorts attributes, namespace declarations first, which helps when comparing two exports.

Savings and when to use it

The first example shrinks from 335 to 263 bytes, about 21%, and deeply indented SOAP envelopes or Android resource files gain more. Minified XML is useful for payloads embedded in JSON strings or database columns, SOAP request templates, and test fixtures you want on a single line. For files that people edit, keep the formatted version in source control and minify at build time.

Press Ctrl/Cmd+Shift+M to switch between minified and formatted output, and Ctrl/Cmd+Shift+C to copy the result. The document is processed entirely in your browser.

Validation comes first

A minifier that guesses on broken markup produces broken markup that is harder to read. Every input is parsed before anything is printed, so a mismatched closing tag, an unescaped &, an unquoted attribute or a second root element stops with an error pointing at the line and column, just as on the XML validator.

Examples

Order export with comments and CDATA

Indentation and the comment disappear, while the CDATA note keeps its extra spaces and the XML declaration stays at the top.

Input
<?xml version="1.0" encoding="UTF-8"?>
<!-- exported 2026-09-14 -->
<orders>
  <order id="ord_8f2k1" status="paid">
    <customer>Aisha Tan</customer>
    <note><![CDATA[Leave at   the door & ring twice]]></note>
    <items>
      <item sku="KB-104" qty="1"/>
      <item sku="MS-220" qty="2"></item>
    </items>
  </order>
</orders>
Output
<?xml version="1.0" encoding="UTF-8"?><orders><order id="ord_8f2k1" status="paid"><customer>Aisha Tan</customer><note><![CDATA[Leave at   the door & ring twice]]></note><items><item sku="KB-104" qty="1"/><item sku="MS-220" qty="2"></item></items></order></orders>
Open this example in the tool

Keep comments and self-close empty elements

The export comment survives, and the empty item element is written as <item sku=“KB-104”/>.

Input
<?xml version="1.0"?>
<!-- exported 2026-09-14 -->
<orders>
  <order id="1">
    <items>
      <item sku="KB-104"></item>
    </items>
  </order>
</orders>
Output
<?xml version="1.0"?><!-- exported 2026-09-14 --><orders><order id="1"><items><item sku="KB-104"/></items></order></orders>
Open this example in the tool

Feed entry with preserved spacing

Whitespace between elements goes, but the padded title text and the xml:space=“preserve” element come through unchanged.

Input
<?xml version="1.0"?>
<feed>
  <title>  Release notes  </title>
  <entry id="1">
    <pre xml:space="preserve">  keep   me  </pre>
  </entry>
</feed>
Output
<?xml version="1.0"?><feed><title>  Release notes  </title><entry id="1"><pre xml:space="preserve">  keep   me  </pre></entry></feed>
Open this example in the tool

Common errors and how to fix them

ErrorCauseFix
Expected </order> to close <order> opened at line 1, found </orders>
Explained
An element was left open, so the closing tags no longer nest correctly.Add the missing closing tag at the right place; the message gives the line where the open element started.
Unescaped '&' — it must start an entity reference like &amp; or &#38;
Explained
A literal ampersand appears in text or an attribute, for example in a company name or a URL query.Write & instead, or wrap longer text in a CDATA section.
Only one root element is allowed — <b> follows the root element <a> that closed at line 1
Explained
Several top-level elements were pasted, such as rows copied out of a larger document.Wrap them in a single parent element before minifying.
The value of attribute 'x' must be in quotesAn attribute was written HTML-style without quotes, which XML does not permit.Quote every attribute value, as in x=“1”.

Frequently asked questions

Does minifying XML change the data?

No. Text content, CDATA, attribute values and entity references are kept exactly; only whitespace between elements in element-only content is removed, plus comments unless you keep them.

Why are there still spaces between some tags?

Those elements have mixed content, where text and child elements share a parent. Spaces there are part of the text a reader sees, so removing them would change the document.

Can I keep comments when I minify?

Yes. Enable Keep comments when minifying in the options and every comment is written back in place.

How do I pretty-print minified XML again?

Switch the toolbar to Beautify, or open the XML formatter. Indentation is added only where it is safe, so the round trip does not alter content.

Related tools