--- id: xmlformatter version: "0.2.9" license: MIT license_treatment: permissive maintenance: active --- # xmlformatter — Format and compress XML documents License: permissive · Maintenance: active · Downloads: 92.2K/mo ## 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 above — 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 pip install xmlformatter uv add xmlformatter 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_current - Install friction: low - Maintenance: active - Downloads: 92.2K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags xml formatting and indentation, xml whitespace normalization, xml minification compression, pretty-print xml documents, xml document cleanup, xml-formatting, cli-tool [View on SkillFed](https://skillfed.io/packages/xmlformatter) · [View on PyPI](https://pypi.org/project/xmlformatter/)