skillfed

xmlformatter

Format and compress XML documents

xmlformatter v0.2.9 92.2K downloads/30d#13,469 on PyPI
Permissive license MIT Active released

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 xmlformatter

uv

uv add xmlformatter

poetry

poetry add xmlformatter

Installing 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

Programming Language :: Python :: 3Topic :: Text Processing :: Markup :: XML

Tags

xml formatting and indentationxml whitespace normalizationxml minification compressionpretty-print xml documentsxml document cleanup
xml-formattingcli-tool

More XML packages