XML minifier — remove whitespace and comments between tags

Paste XML to get a one-line copy with the indentation and comments between tags removed, after a well-formedness check, and see how many bytes you saved.

Live demo · real result from this tool

Minified XML

<?xml version="1.0"?><feed><item id="1"><name>Desk lamp</name><price>24.99</price></item></feed>
Saved29.4%
Original
136 B
Minified
96 B

Processed on your device — nothing is uploaded.

How to use the XML minifier

  1. Paste your document into the XML box. A small product feed is there as a sample.
  2. Keep Remove comments ticked to drop <!-- … --> comments, or untick it to keep them.
  3. The Minified XML appears below with Saved, Original and Minified sizes.
  4. Copy the result or download it as minified.xml.

What it does and when to use it

Pretty-printed XML carries a lot of indentation that no program reads. Minifying it makes API payloads, SOAP requests, sitemaps, config blobs and feed files smaller, and puts a document on one line so it fits in a log entry, a test fixture or a string constant.

To read a minified file again, use the XML formatter. To move between formats, try XML to JSON or JSON to XML. The SQL minifier does a similar clean-up for database queries.

How it works

First, fast-xml-parser’s validator checks that every tag is closed and nested correctly. If not, you see the line, column and reason, and nothing is minified.

Then the tool’s own tokenizer splits the document into tags, text, comments, CDATA and declarations, and builds the element tree. While writing it out again it:

  • drops text nodes that are only whitespace,
  • keeps every text node with visible characters exactly as written,
  • rewrites each start tag with single spaces between attributes and no space before > or />,
  • removes comments when the box is ticked,
  • keeps all whitespace inside an xml:space="preserve" element.

All of this runs in your browser; the XML is not uploaded.

Worked examples

These are the tool’s own outputs.

The sample feed. With comments removed, the 136-byte sample becomes 96 bytes, Saved: 29.4%:

<?xml version="1.0"?><feed><item id="1"><name>Desk lamp</name><price>24.99</price></item></feed>

With Remove comments unticked, <!-- product feed --> stays and the saving drops to 14%.

Text with its own line breaks. This note has a padded start tag and a multi-line body:

<note>
  <to   lang="en"  >Asha</to>
  <body>
    Meet at 10.
  </body>
  <logo src="a.png" />
</note>

The result is <to lang="en"> and <logo src="a.png"/> on one line, but the body keeps its newlines because they are part of its text.

Badly nested tags. <a><b></a> returns “XML is not well-formed at line 1, column 7: Expected closing tag ‘b’…”.

Limits and tips

  • Whitespace inside values stays. The tool cannot know whether spaces in your text matter, so it never trims them.
  • No renaming or shortening. Namespace prefixes, attribute quotes and entity references are left as they are.
  • Compression helps more. Gzip on the server usually saves far more than minifying; the two work together.
  • Keep the readable original for editing, and minify a copy.

Frequently asked questions

Can minifying change what my XML means?
It only removes whitespace-only text between tags, comments (if you choose) and extra spaces inside tags. Text that contains any visible character is copied exactly, including its spaces and line breaks, so element values do not change.
Why is there still a line break in my output?
The break is part of an element's text. In <body> with "Meet at 10." on its own line, the newlines belong to the value, so the tool keeps them. Remove them in your source if the value should be on one line.
Are CDATA sections and the XML declaration kept?
Yes. The <?xml ?> declaration, processing instructions, DOCTYPE and CDATA sections are copied as written. Elements with xml:space="preserve" keep all their inner whitespace.
Does it check that the XML is valid?
It checks that the XML is well-formed with fast-xml-parser's validator and reports the line and column of the first problem. It does not validate against a schema or DTD.
How much will my file shrink?
That depends on how deeply it is indented and how many comments it has. The Saved, Original and Minified figures show the exact result for your file.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close