data-to-xml

raw JSON →
1.0.9 verified Fri May 01 auth: no python

A simple Python library for converting dictionaries to XML strings. Current version 1.0.9, updated periodically on PyPI.

pip install data-to-xml
error ImportError: cannot import name 'dicttoxml' from 'dicttoxml'
cause Wrong import path; package 'data-to-xml' exposes 'dicttoxml' from its submodule, but the correct import is 'from dicttoxml import dicttoxml'.
fix
Use 'from dicttoxml import dicttoxml' or 'import dicttoxml.dicttoxml'.
error TypeError: a bytes-like object is required, not 'str'
cause The dicttoxml() result is bytes; concatenating with str without decode.
fix
Call .decode('utf-8') on the result or encode your other strings.
error ValueError: Invalid root element name
cause The root element name contains spaces or special characters not allowed in XML.
fix
Ensure custom_root is a valid XML tag name (no spaces, starts with letter/underscore).
gotcha The function returns bytes (not str) by default in Python 3. You must decode if you expect a string.
fix Use .decode('utf-8') on the result or wrap with str().
gotcha The 'attr_type' parameter defaults to True, which adds 'type' attributes (e.g., type="str") to each element. This often breaks XML schemas or downstream parsers.
fix Explicitly set attr_type=False when calling dicttoxml().
deprecated The 'root' parameter (to set root element name) has been deprecated in favor of 'custom_root'.
fix Use custom_root='your_name' instead of root='your_name'.

Convert a dict to XML with optional root element and type attributes disabled.

from dicttoxml import dicttoxml

my_dict = {
    'name': 'Alice',
    'age': 30,
    'items': ['book', 'pen']
}

xml_str = dicttoxml(my_dict, custom_root='person', attr_type=False)
print(xml_str.decode() if isinstance(xml_str, bytes) else xml_str)