Skip to content

XML Minifier

Minified XML —
Original size (bytes)
—
Minified size (bytes)
—
Bytes saved
—
Size reduction
—
Elements
—
Comments removed
—

An XML minifier removes the indentation and line breaks between tags, optional comments, and extra spaces inside tags, without touching text values, CDATA or quoted attributes. For example, an indented 19-byte document with root a and one child b holding 1 shrinks to 15 bytes, a 21.1% cut, with identical parsed content.

About this tool

Pretty-printed XML is easy to read but carries a lot of dead weight: every level of nesting adds a newline and a run of spaces that a parser throws away. Developers trimming SOAP payloads, sitemaps, Android resource files or config bundles shipped inside an app usually want that weight gone. Paste or open a file and the minified XML appears straight away, along with the original and new size in UTF-8 bytes, the percentage saved, the element count and how many comments were removed. The minifier also checks that every opening tag has a matching closing tag and names the first one that does not. It is not a full validating parser: it does not check namespaces, entity definitions or a schema, and it assumes whitespace between elements is insignificant, which is wrong for mixed content such as XHTML paragraphs.

How to use it

  1. Add your XML

    Paste the document into the box, or use Open a text file to load a .xml, .svg or .rss file from your device. The file is read locally.

  2. Choose about comments

    Leave Remove XML comments ticked to drop every <!– –> block, or untick it to keep licence headers and notes.

  3. Decide on text trimming

    Trim spaces around text values strips leading and trailing spaces in element text. Untick it when those spaces carry meaning.

  4. Copy the result

    Read the minified XML and the size figures, then copy the output into your file or request body.

Examples

An indented two-level document

XML to minify
<a> <b>1</b> </a>

Result Minified XML: <a><b>1</b></a>
Original size (bytes): 19
Minified size (bytes): 15
Bytes saved: 4
Size reduction: 21.1%
Elements: 2
Comments removed: 0

  1. Original size: 19 bytes (UTF-8).
  2. Removed whitespace-only text between tags.
  3. Minified size: 15 bytes, saving 4 bytes (21.1%).

The newline and two-space indent around the child element disappear, leaving 15 of the original 19 bytes.

A note with a comment

Remove XML comments
Yes
XML to minify
<note> <!– draft –> <to>Ana</to> </note>

Result Minified XML: <note><to>Ana</to></note>
Original size (bytes): 46
Minified size (bytes): 25
Bytes saved: 21
Size reduction: 45.7%
Elements: 2
Comments removed: 1

  1. Original size: 46 bytes (UTF-8).
  2. Removed whitespace-only text between tags and 1 comment(s).
  3. Minified size: 25 bytes, saving 21 bytes (45.7%).

The comment is dropped along with the indentation, and the two elements are counted.

Attributes with extra spaces

XML to minify
<list> <item id="1" name="a b" /> </list>

Result Minified XML: <list><item id="1" name="a b"/></list>
Original size (bytes): 47
Minified size (bytes): 39
Bytes saved: 8
Size reduction: 17%
Elements: 2
Comments removed: 0

  1. Original size: 47 bytes (UTF-8).
  2. Removed whitespace-only text between tags.
  3. Minified size: 39 bytes, saving 8 bytes (17%).

Runs of spaces between attributes shrink to one, but the double space inside the quoted value stays as written.

How it is calculated

Saving % = (original bytes − minified bytes) ÷ original bytes × 100

original bytes
UTF-8 size of the XML you pasted
minified bytes
UTF-8 size after whitespace and optional comments are removed

The input is split into tags, comments, CDATA sections, processing instructions, declarations and text. Text runs made only of whitespace are dropped, following the XML 1.0 rule that such whitespace in element-only content is insignificant. Inside tags, runs of spaces and newlines collapse to a single space, but anything between quotes is copied unchanged. CDATA is never altered. Sizes are counted in UTF-8 bytes, so an accented letter counts as two bytes and most emoji as four.

When not to use it

  • Skip it for mixed content such as XHTML or DocBook, where spaces between inline tags are part of the text.
  • Do not use it to validate a document against an XSD or DTD; it only checks tag balance.
  • Avoid it on files with xml:space="preserve" regions, which it does not honour.

Common mistakes

  • Minifying a file you still edit by hand, then losing the readable copy.
  • Leaving comment removal on when a licence header must stay in the shipped file.
  • Expecting big savings on data-heavy XML, where most bytes are values rather than indentation.

Frequently asked questions

Does minifying change how a parser reads my XML?

For element-only content, no. A parser reports the same elements, attributes and text values; only the whitespace-only nodes between elements disappear. If your code reads those whitespace nodes, for example by counting child nodes rather than child elements, the count will change.

Are CDATA sections touched?

No. Everything between <![CDATA[ and ]]> is copied exactly, including line breaks and indentation, because CDATA content is character data the receiving application sees verbatim.

What happens to the XML declaration and DOCTYPE?

They are kept, with internal runs of whitespace collapsed to single spaces. The declaration must stay first in the document, so the minifier never moves or removes it.

Why does it report a mismatched tag?

While minifying, each opening tag is pushed onto a stack and each closing tag must match the most recent open one. If the closing tag for a arrives while b is still open, or an element is never closed, the tool stops and names the tag so you can fix the source first.

How much smaller will my file get?

It depends on how deeply indented the document is and how long its values are. A heavily nested config with short values often shrinks by 20 to 40 percent, while a feed full of long descriptions may save under 10 percent. The size figures show the exact result for your input.

Does it work with SVG files?

Yes, SVG is XML, so indentation and comments are removed the same way. Text inside SVG text elements is trimmed when that option is on, so untick it if leading spaces in a label matter.

Is minified XML better than gzip?

They work together. Gzip already compresses repeated whitespace well, so minifying mostly helps where compression is not applied, such as embedded strings, database fields or size-limited message queues. Combining both gives the smallest transfer.