dicttoxml
Convert Python dictionaries and native data types into valid XML strings
Repository Health
Technical Analysis
dicttoxml is a small, dependency-free Python library that turns dictionaries, lists, sets, tuples, and other native data types into valid XML strings. It supports arbitrary nesting of collections and maps common scalar types (int, float, Decimal, bool, str, datetime, None) to typed XML elements.
The single dicttoxml() function is highly configurable: you can emit full documents or snippets, name the root element, toggle type attributes, add unique element IDs, change the encoding, and customize list-item element names. This makes it a convenient bridge for producing XML output from JSON-like data structures.
What You Get
- A single
dicttoxml()function that converts any supported native data type into XML - Optional per-element
typeattributes recording the original Python type - Configurable output: full document vs. snippet, custom root element, encoding control, and unique element IDs
- Custom list-item element naming via a user-supplied function
- Zero third-party runtime dependencies (pure standard library)
Common Use Cases
- Converting JSON payloads fetched from an API into XML for legacy systems
- Generating XML snippets to embed inside a larger XML document
- Serializing configuration or report data held in Python dicts as XML
- Producing typed XML output where consumers need to know the original data type
Under The Hood
Architecture dicttoxml is a single 474-line module (dicttoxml.py) built around a recursive convert() dispatcher that inspects each value’s type and routes it to a type-specific converter (convert_kv, convert_list, convert_dict, convert_none). Output is assembled as concatenated strings rather than a DOM tree, with an optional xml.dom.minidom pass used only for pretty-printing examples. Tech Stack Pure Python targeting 3.6+, relying entirely on the standard library (numbers, logging, collections.abc, xml.dom.minidom, random); packaged via a classic distutils setup.py exposing a single py_modules entry with no third-party runtime dependencies. Code Quality The code is compact and readable with docstrings and logging throughout, but carries legacy Python 2/3 compatibility shims (unicode/long fallbacks) and ships no automated test suite in the repository, relying instead on README-documented behavior. API Design The public surface is a single well-documented dicttoxml() function with sensible defaults and a handful of keyword flags (root, custom_root, attr_type, ids, item_func, encoding), making it trivial to adopt while still offering fine-grained control for advanced output shaping.