dict2xml

Tiny dependency-free utility to convert Python dictionaries into XML strings.

Library
PyPI
v1.7.8
33stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
30/100Needs Attention
Development Activity4
Maintenance20
Community24
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture70
Code Quality74
Innovation55
Learning Curve90

dict2xml is a small Python utility that converts a nested dictionary into a well-formed XML string. You pass in ordinary Python data — dicts, lists, and scalars — and it walks the structure to emit matching XML elements, expanding list values into repeated sibling nodes and nested dicts into nested elements.

The library is intentionally minimal and has no third-party dependencies. It gives you a couple of entry points — a function that returns an XML string and a lower-level node builder — plus options for wrapping the document in a root element, controlling indentation, and validating element names against XML naming rules.

What You Get

  • A dict2xml(data, ...) function that returns a formatted XML string from a Python dictionary
  • A lower-level Node/converter API for building XML output programmatically
  • Automatic expansion of list values into repeated sibling elements
  • Options for a wrapping root element (wrap) and custom indentation (indent)
  • XML name validation using proper NameStartChar/NameChar character ranges

Common Use Cases

  • Serializing configuration or API payloads from Python dicts into XML
  • Generating XML documents for legacy systems that require XML input
  • Quickly producing readable, indented XML for debugging or fixtures

Under The Hood

Architecture — The whole library lives in dict2xml/logic.py, exposed through __init__.py. It defines a small set of node classes and a DataSorter/converter that recursively walks the input structure: dict keys become element names, list values expand into repeated sibling nodes, and scalars become text. Precompiled regexes for NameStartChar/NameChar (the XML 1.0 name production) validate element names, and a rendering pass applies wrapping and indentation to produce the final string. Companion .pyi stubs and py.typed ship inline type information.

Tech Stack — Pure Python using only the standard library (collections, re), packaged with pyproject.toml. Development tooling includes tox, pytest, and pylama linting, but there are no runtime dependencies.

Code Quality — For a small utility the project is carefully maintained: a tests/ suite, type stubs, linting config, and tox matrix testing back a codebase that has existed since 2012. The XML-name regex handling shows attention to correctness beyond a naive string join.

API Design — The surface is deliberately tiny — import dict2xml and call it with your data plus optional wrap and indent arguments — so there is almost nothing to learn. A lower-level node API is available for callers who need finer control, and the README’s before/after examples make usage immediately clear.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search