xmlformatter
Format and compress XML documents
What it is and what it does
xmlformatter is a Python 3 XML formatting library that normalizes whitespace in XML documents according to a set of explicit rules. It distinguishes between element content (treated as structured data) and mixed content (treated as text), applying different formatting strategies to each. For element content, it removes leading and trailing whitespace and collapses whitespace sequences; for mixed content, it preserves text flow while normalizing spacing around inline elements. The package provides both a programmatic API via the Formatter class and a command-line tool (xmlformat) for standalone use.
The library supports customization through options like indentation width and character, output encoding, element preservation (to skip formatting subtrees), and compression modes. It can read from files, strings, or stdin, and write to files or stdout. The package is a Python 3 replacement for the older Python 2 XmlFormatter package and includes migration guidance for existing users.
Use it for:
- Pretty-print XML configuration files for readability and version control diffs
- Minify XML documents to reduce file size for transmission or storage
- Normalize whitespace in mixed-content XML (like XHTML or DocBook) while preserving text semantics
- Automate XML formatting in build pipelines or pre-commit hooks via the command-line tool
- Preserve specific XML subtrees (e.g. code blocks or literal content) while formatting the rest of a document
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Formats and compresses XML documents by applying distinct whitespace rules that treat element content as objects and mixed content as text, with options to preserve specific elements and customize indentation.
Yes. xmlformatter is a lightweight, actively maintained tool with no external dependencies, a permissive MIT license, and no known vulnerabilities. Install it if you need to format, normalize, or compress XML documents—either programmatically or from the command line. The distinct handling of element vs. mixed content makes it useful for documents that blend structured data and natural language text.
Install
xmlformatter on PyPI
pip
pip install xmlformatteruv
uv add xmlformatterpoetry
poetry add xmlformatterInstalling xmlformatter
Before you install
Low install friction with no runtime dependencies. Active maintenance status with a recent release.
License in practice
MIT license permits use, modification, and distribution with minimal restrictions, making it suitable for both open-source and proprietary projects.
Quickstart
import xmlformatter
formatter = xmlformatter.Formatter(indent="2", indent_char=" ")
formatter.format_file("/path/to/doc.xml")
Verify before relying
- Whether the package handles large XML files efficiently or has memory constraints
- Performance characteristics when formatting deeply nested XML structures
- Compatibility with XML schemas or DOCTYPE declarations
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.2) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 100 days since the last release |
| First released | |
| Downloads | 92,197/month — #13,469 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: xmlformatter-0.2.9-py3-none-any.whl
Tags
More XML packages
Beautiful Soup parses HTML and XML documents…
permissive · top 100 on PyPI
lxmllxml provides Python bindings to libxml2 and…
permissive · top 1,000 on PyPI
defusedxmlDefusedxml hardens Python's standard XML…
permissive · top 1,000 on PyPI
docutilsDocutils converts plaintext documentation in…
unclear · top 1,000 on PyPI
xmltodictConverts XML to Python dictionaries and back,…
permissive · top 1,000 on PyPI
SphinxSphinx generates professional documentation…
permissive · top 1,000 on PyPI
paragraphsProvides a single function that joins…
permissive · top 15,000 on PyPI
prettierfierReformats HTML and XML strings with intelligent…
permissive · top 15,000 on PyPI
dicttoxmlConverts Python dictionaries and other native…
copyleft · top 5,000 on PyPI
compact-jsonFormats JSON into compact, human-readable…
permissive · top 5,000 on PyPI
dict2xmlConverts Python dictionaries into XML strings…
permissive · top 5,000 on PyPI
htmlmin2Minifies HTML by removing unnecessary…
permissive · top 5,000 on PyPI
meld3meld3 is an HTML/XML templating system that…
permissive · top 15,000 on PyPI
et-xmlfileet_xmlfile writes large XML files with minimal…
permissive · top 1,000 on PyPI
objprintobjprint provides functions to print Python…
permissive · top 5,000 on PyPI