Registry / serialization / dict2xml

dict2xml

JSON →
library1.7.8pypypi✓ verified 25d ago

dict2xml is a small Python utility designed to convert a Python dictionary into an XML string. It is actively maintained, with frequent minor releases. The current version is 1.7.8.

pip install dict2xml
INSTALL
IMPORT
SIG · DICT2XML
D
dict2xml
serializationpythonv1.7.8
Install
1.5s avg
Import
91ms
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.8 · 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.092s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.090s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

dict2xml
from dict2xml import dict2xml
The primary function to convert a dictionary to XML.

This quickstart demonstrates how to convert a nested Python dictionary into an XML string using the `dict2xml` function. It uses the `wrap` parameter to specify a root element and `indent` for readability.

from dict2xml import dict2xml data = { 'root_element': { 'item1': 123, 'item2': [ {'sub_item_a': 'hello'}, {'sub_item_b': 'world'} ], 'item3': { 'nested_value': 'example' } } } xml_string = dict2xml(data, wrap='root', indent=' ') print(xml_string)
Debug
Known issues
gotchaThe library does not support adding attributes directly to XML elements. Dictionary keys are treated as element names, and their values as element content.
fix
Consider using a different library if XML attributes are a hard requirement, or structure your dictionary to represent attributes as child elements if possible. For example, `{ 'element': { 'attribute_key': 'attribute_value', 'content': 'text' } }` will result in nested elements, not an element with an attribute.
affects: All versions
gotchaBy default, `dict2xml` sorts dictionary keys alphabetically when converting them to XML elements, which can lead to unexpected ordering if not explicitly handled. `OrderedDict` keys are *not* sorted by default.
fix
If key order is important and you are not using an `OrderedDict`, you can provide a `data_sorter` to the `Converter` class. For example, `Converter(data_sorter=DataSorter.never()).build(data)` to prevent sorting keys, or `data_sorter=DataSorter.always()` to force sorting, even for `OrderedDict`.
affects: All versions up to 1.7.8
breakingAs of version 1.7.0, `dict2xml` officially dropped support for Python 2. It is now only supported for Python 3.6+ (though it may still work in 3.5).
fix
Ensure your project is running on Python 3.6 or newer. If you need Python 2 compatibility, you must use an older version of the `dict2xml` library (e.g., < 1.7.0).
affects: 1.7.0 and later
gotchaThe library does not automatically include an XML declaration line (e.g., `<?xml version="1.0" encoding="UTF-8"?>`) in the generated XML string.
fix
If an XML declaration is required, you must prepend it manually to the output string.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dict2xml'
The 'dict2xml' library is not installed in your Python environment or there is a typo in the import statement.
fix
Install the library using pip: `pip install dict2xml`
AttributeError: module 'dict2xml' has no attribute 'convert'
When using `import dict2xml`, the main conversion function is also named `dict2xml`, or the user might be attempting to call a non-existent method like `convert` instead of the correct `dict2xml()` function or `Converter().build()` method.
fix
After `import dict2xml`, call the function as `dict2xml.dict2xml(your_dict)` or use `from dict2xml import dict2xml` and call it as `dict2xml(your_dict)`. Alternatively, if using the class-based approach, import `Converter` and use `Converter().build(your_dict)`.
TypeError: Elements with an unsupported data type raise a TypeError exception.
The dictionary contains a value with a Python data type that 'dict2xml' does not explicitly know how to convert into a valid XML element, such as a custom object without proper serialization defined or certain unsupported native types.
fix
Ensure all values in your dictionary are of supported types (e.g., strings, numbers, booleans, lists, dictionaries, None, datetime objects) or convert unsupported types to strings before passing the dictionary to `dict2xml`.
Dictionary order not maintained in XML output
Prior to Python 3.7, standard dictionaries did not guarantee insertion order. While Python dictionaries now maintain order, 'dict2xml' (or its predecessor 'dicttoxml') might sort keys by default, leading to an unexpected order in the XML output.
fix
Use a `collections.OrderedDict` for your input data and pass `data_sorter=DataSorter.never()` to `dict2xml.Converter().build()` or `data_sorter=False` if directly calling the `dict2xml` function to prevent key sorting, ensuring the original order is preserved.

```python
from dict2xml import dict2xml, Converter
from collections import OrderedDict

data = OrderedDict([('b', 2), ('a', 1)])
xml_string = dict2xml(data, data_sorter=False) # For the top-level function if it supports it
# Or using the Converter class explicitly:
converter = Converter()
xml_string = converter.build(data, data_sorter=Converter.DataSorter.never())
```
Upgrade
Version history
1.7.8latest on PyPI · released Jan 14, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
dict2xml — pip install dict2xml · libregistry