Overview
Introduction
A hand-formatted or prettified XML document carries a lot of whitespace that only exists for human readability, indentation, line breaks, and blank lines between elements. None of that is needed for the document to parse correctly, so before shipping it over a network or storing it at scale, stripping it out is free savings.
This tool takes any well-formed XML and re-serializes it with all insignificant whitespace removed, keeping every element, attribute, and comment intact.
What Is XML Minifier?
An XML minifier parses a document into its underlying tree of elements and text, then re-serializes it with no whitespace between tags: no indentation, no line breaks, no blank lines.
It's the XML equivalent of a JSON minifier: the data model is unchanged, only its on-the-wire or on-disk representation shrinks.
How XML Minifier Works
Your input is parsed into a tree of element, text, comment, CDATA, and processing-instruction nodes using a stack-based parser, then walked and re-emitted with each node written directly adjacent to the next, trimming leading and trailing whitespace from text content along the way.
The tool also reports the original size, minified size, and percentage saved, computed from the UTF-8 byte length of each version, so you can see exactly how much smaller the result is.
When To Use XML Minifier
Use it before embedding an XML payload in a config value, environment variable, or a place where every byte counts.
It's also useful when preparing a document to send over a network where response size affects latency, or when you want a single-line version to paste into a log statement or test fixture.
Often used alongside XML Prettifier, XML Validator and XML Syntax Highlighter.
Features
Advantages
- Runs entirely client-side, so documents with real configuration or customer data never leave your browser.
- Preserves comments, processing instructions, and attribute order exactly, nothing is silently dropped.
- Reports exact byte savings so you can quantify the benefit.
- Confirms the document is well-formed as a side effect of minifying.
Limitations
- Only removes whitespace; it doesn't rename elements, shorten attribute names, or otherwise restructure the document the way a lossy compressor would.
- If the original document relies on whitespace as significant text content (rare, but possible inside elements meant to preserve formatting), minifying will still trim that text, since the tool can't distinguish intent from insignificant padding.
- Very large files (tens of megabytes) may be slow, since the whole document is held in memory while parsing.
Examples
Best Practices & Notes
Best Practices
- Keep a prettified copy of any document you minify for source control or editing; treat the minified version as a build output.
- Minify right before transfer or storage, not as a working copy you'll need to edit by hand later.
- Run the Validator first if you're unsure the document is well-formed, so an unexpected error doesn't interrupt a pipeline.
Developer Notes
This implementation shares the same parser as the XML Prettifier (`parseXmlDocument`), so both tools agree on what counts as a well-formed document and where a parse error is reported. The minifying serializer walks the same node tree but concatenates every node with no separators and trims whitespace-only text nodes to nothing, rather than indenting them onto their own line.
XML Minifier Use Cases
- Shrinking a config file before storing it as an environment variable
- Compacting an XML payload before pasting it into a one-line log statement
- Preparing a minimal test fixture for a unit test
- Reducing the size of an XML document before sending it over a slow connection
Common Mistakes
- Minifying a document and then losing the original, readable copy needed for future edits.
- Assuming minifying validates the document; a document must already be well-formed for minifying to succeed.
- Expecting element or attribute names to be shortened; this tool only removes whitespace, it doesn't rename anything.
Tips
- Check the reported byte savings to decide whether minifying is worth it for a given document.
- Use the XML Prettifier to reverse this and get a readable copy back for editing.
- Upload a `.xml` file directly instead of copy-pasting if the document is large.