Last updated
What Does the XML Minifier Do?
The XML Minifier removes unnecessary whitespace, indentation, line breaks, and optionally comments from XML files while preserving the complete data structure. Minified XML is 30–50% smaller, faster to transmit, and quicker to parse — ideal for production APIs and data exchange.
What Gets Removed
Removed (safe to remove):
✓ Whitespace between elements (indentation, line breaks)
✓ XML comments (optional — can be preserved)
✓ Redundant whitespace in attribute values
Preserved (never removed):
✗ XML declaration (<?xml version="1.0"?>)
✗ Text content within elements
✗ Significant whitespace in text nodes
✗ CDATA sections (content preserved exactly)
✗ Processing instructions
✗ Namespace declarations
✗ Entity references (&, <, etc.)
CDATA Section Handling
Input:
<script>
<![CDATA[
function hello() {
console.log("Hello, World!");
}
]]>
</script>
Output (CDATA content preserved exactly):
<script><![CDATA[
function hello() {
console.log("Hello, World!");
}
]]></script>
Note: Whitespace INSIDE CDATA is never removed —
it may be significant (e.g., code indentation).
Comment Handling Options
Option 1: Remove all comments (default)
Input: <!-- Database config --><host>localhost</host>
Output: <host>localhost</host>
Option 2: Preserve all comments
Input: <!-- Database config --><host>localhost</host>
Output: <!-- Database config --><host>localhost</host>
Option 3: Preserve license/copyright comments only
Preserves: <!-- Copyright 2024 Example Corp. -->
Removes: <!-- TODO: fix this -->
SOAP Message Minification
Input SOAP message (1,240 bytes):
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://example.com/service">
<soap:Header>
<tns:AuthToken>abc123</tns:AuthToken>
</soap:Header>
<soap:Body>
<tns:GetUser>
<tns:UserId>42</tns:UserId>
</tns:GetUser>
</soap:Body>
</soap:Envelope>
Minified (680 bytes — 45% smaller):
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://example.com/service"><soap:Header><tns:AuthToken>abc123</tns:AuthToken></soap:Header><soap:Body><tns:GetUser><tns:UserId>42</tns:UserId></tns:GetUser></soap:Body></soap:Envelope>
File Size Comparison
File type Typical reduction
--------- -----------------
Config files 30–40%
SOAP messages 40–50%
Data exports 35–45%
SVG files 20–30%
RSS/Atom feeds 25–35%
Example: 10,000 API calls/day with 5KB XML responses
Before minification: 50 MB/day bandwidth
After minification: 30 MB/day bandwidth (40% reduction)
Monthly savings: ~600 MB bandwidth
XML Minification in Build Pipeline
# Node.js — minify XML files in build
const { minify } = require('xml-minifier');
const fs = require('fs');
const input = fs.readFileSync('config.xml', 'utf8');
const minified = minify(input, {
removeComments: true,
collapseWhitespace: true,
preserveCDATA: true
});
fs.writeFileSync('config.min.xml', minified);
# Python
import re
def minify_xml(xml_string, remove_comments=True):
if remove_comments:
xml_string = re.sub(r'<!--.*?-->', '', xml_string, flags=re.DOTALL)
# Remove whitespace between tags
xml_string = re.sub(r'>\s+<', '><', xml_string)
# Remove leading/trailing whitespace
return xml_string.strip()
Common Use Cases
- Reducing SOAP web service message sizes for faster transmission
- Minifying XML API responses in production
- Compressing XML data exports before storage or transfer
- Optimizing XML configuration files read frequently by applications
- Reducing bandwidth costs for high-volume XML data exchange