Registry / serialization / dicttoxml

dicttoxml

JSON →
library1.7.16pypypi✓ verified 26d ago

dicttoxml is a Python library that converts a Python dictionary or other native data types (like lists, sets, tuples, integers, strings, booleans, and datetime objects) into a valid XML string. It supports arbitrary nesting for collections and offers options to control XML declaration, root element, and type attributes. The current version is 1.7.16, actively maintained to support Python 3.6 and forward versions.

pip install dicttoxml
INSTALL
IMPORT
SIG · DICTTOXML
D
dicttoxml
serializationpythonv1.7.16
Install
1.6s avg
Import
76ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.7.16 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.082s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.070s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dicttoxml
from dicttoxml import dicttoxml
dicttoxml module
import dicttoxml
import dicttoxml2
While 'dicttoxml2' is a compatible fork, the user specifically requested 'dicttoxml'. Ensure you import the correct library you've installed.

This quickstart demonstrates how to convert a Python dictionary into an XML string using `dicttoxml` and then pretty-prints the output using `xml.dom.minidom` for readability.

from dicttoxml import dicttoxml import xml.dom.minidom my_dict = { 'name': 'Alice', 'age': 30, 'is_student': False, 'courses': [ {'title': 'Math', 'credits': 3}, {'title': 'Science', 'credits': 4} ], 'address': {'street': '123 Main St', 'city': 'Anytown'} } # Convert dictionary to XML with default settings xml_bytes = dicttoxml(my_dict) # Pretty print the XML (optional) dom = xml.dom.minidom.parseString(xml_bytes) pretty_xml_string = dom.toprettyxml() print(pretty_xml_string)
Debug
Known issues
gotchaBy default, `dicttoxml` includes a `type` attribute for each element (e.g., `<name type="str">`). This can lead to verbose XML if not desired.
fix
Pass `attr_type=False` to the `dicttoxml` function: `dicttoxml(my_dict, attr_type=False)`.
affects: All versions (behavior since 1.4).
gotchaThe converted XML is wrapped in a `<root>` element by default, and includes an XML declaration (`<?xml version="1.0" encoding="UTF-8" ?>`).
fix
To change the root element, use `custom_root='my_custom_tag'`. To generate an XML snippet without the root or declaration, pass `root=False` to `dicttoxml()`.
affects: All versions (behavior since 0.4 for root, 1.5 for custom_root).
gotchaElement names that are not valid XML (e.g., contain spaces or special characters) are automatically converted. Spaces become underscores, and severely invalid names become `<key name="original-invalid-name">`.
fix
Sanitize dictionary keys to be valid XML element names before conversion, or be aware of `dicttoxml`'s automatic handling.
affects: All versions.
gotchaThe library enables verbose logging by default, which can clutter console output or log files with INFO-level messages for every conversion step.
fix
Disable debugging output by calling `dicttoxml.set_debug(False)` or configure Python's `logging` module to suppress `dicttoxml`'s output.
affects: All versions (issue reported in 2017).
Errors
Common errors & fixes
TypeError: Unsupported data type: <unsupported_object> (<class 'unsupported_type'>)
The `dicttoxml` library encountered a data type within the dictionary (or its nested structures) that it does not know how to convert into an XML element.
fix
Ensure that all values in your dictionary are of supported types (e.g., int, float, str, bool, datetime, None, list, dict, set, tuple). If you have a custom or unsupported object, convert it to a string or another compatible type before passing it to `dicttoxml`.
AttributeError: module 'dicttoxml' has no attribute 'dicttoxml'
This error typically occurs when Python attempts to import the `dicttoxml` module but, due to a naming conflict or incorrect file structure, it ends up loading the module's package directory instead of the actual `dicttoxml` function within the `dicttoxml.py` file. This often happens if a local file or directory is named `dicttoxml.py` or `dicttoxml` within the Python path, shadowing the installed library.
fix
Modify your import statement from `import dicttoxml` to `from dicttoxml import dicttoxml`. Additionally, check your project directory and Python path for any files or folders named `dicttoxml.py` or `dicttoxml` that might be conflicting with the installed library.
b'<xml_string>'
The `dicttoxml.dicttoxml()` function returns the XML as a byte string (prefixed with `b`), not a standard UTF-8 string, which can be unexpected when trying to print or manipulate the output as a regular string.
fix
Decode the byte string using `.decode('utf-8')` to convert it into a standard UTF-8 string for display or further processing. Example: `xml_bytes = dicttoxml(my_dict); xml_string = xml_bytes.decode('utf-8')`.
Upgrade
Version history
1.7.16latest on PyPI · released Dec 23, 2022
Audit
Dependencies
pythonrequiredRuntime dependency, requires Python >=3.6.
setuptoolsrequiredBuild/installation dependency.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
dicttoxml — pip install dicttoxml · libregistry