dict2xml
Tiny dependency-free utility to convert Python dictionaries into XML strings.
Repository Health
Technical Analysis
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.