Home / Developer tools / XML minifier

XML Minifier

Remove the line breaks, indentation and comments from XML and put it on one line. Mixed content and CDATA are never changed.

Options

Minified XML

What the XML minifier removes

  • Indentation and line breaks between elements. In element-only content, these spaces are not data.
  • Comments, unless you turn the option off.
  • The spaces around text in simple elements, when the Trim option is on.
  • The XML declaration, if you choose to remove it.

Example

<order id="42">
  <!-- customer data -->
  <customer>Alex</customer>
  <items>
    <item sku="A1" qty="2"/>
    <item sku="B7" qty="1"/>
  </items>
</order>

becomes:

<order id="42"><customer>Alex</customer><items><item sku="A1" qty="2"/><item sku="B7" qty="1"/></items></order>

What is never changed

  • Mixed content, such as <p>Ships in <em>2 days</em> worldwide.</p>. The spaces here belong to the text, so the minifier copies the element exactly.
  • Elements with xml:space="preserve".
  • CDATA sections, processing instructions and the DOCTYPE.
  • The names, attributes and values of every element.

Why minify XML?

  • To make a request or a message smaller, for example a SOAP call or a message in a queue.
  • To put an XML document in a single line of a log, a config value or a database field.
  • To remove comments that you do not want to ship.

Keep your expectations realistic. When a server sends XML with gzip compression, as most do, the white space is already compressed very well, so minifying makes a smaller difference on the network than it makes on disk. The tool shows both numbers, the minified size and the gzip size, so that you can see the real saving.

Trim spaces: one thing to know

With Trim on, the spaces at the start and the end of the text in a simple element are removed, so <name> Alex </name> becomes <name>Alex</name>. This is what most people want. If your data uses those spaces, switch Trim off. Then the text is copied as it is.

Options

Remove or keep comments, trim text or not, sort attributes, use double quotes for attributes, write empty elements as <a/> or as <a></a>, and keep, add or remove the XML declaration.

To read minified XML again, use the XML beautifier or the XML formatter. The XML formatting guide explains when to minify. Other minifiers: HTML minifier, CSS minifier and JSON minifier.

Frequently asked questions

How do I minify XML?

Paste the XML. The line breaks, indentation and comments are removed at once, and the result is on one line. Press Copy result to use it.

Is it safe to minify XML?

The data, the attributes and the structure do not change. Mixed content, CDATA and elements with xml:space="preserve" are copied exactly. Only spaces between elements and comments are removed.

Does minifying XML make it much smaller?

On disk, noticeably for deeply indented files, and the tool shows the exact saving for yours. Over the network with gzip, the difference is small, because gzip already compresses white space well.

Can I keep the comments?

Yes. Untick Remove comments, and the comments stay in the minified XML.