{"id":2924,"library":"dicttoxml","title":"dicttoxml","description":"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.","status":"active","version":"1.7.16","language":"en","source_language":"en","source_url":"https://github.com/quandyfactory/dicttoxml","tags":["xml","dictionary","serialization","data conversion","xml generation"],"install":[{"cmd":"pip install dicttoxml","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Runtime dependency, requires Python >=3.6.","package":"python","optional":false},{"reason":"Build/installation dependency.","package":"setuptools","optional":false}],"imports":[{"symbol":"dicttoxml","correct":"from dicttoxml import dicttoxml"},{"note":"While 'dicttoxml2' is a compatible fork, the user specifically requested 'dicttoxml'. Ensure you import the correct library you've installed.","wrong":"import dicttoxml2","symbol":"dicttoxml module","correct":"import dicttoxml"}],"quickstart":{"code":"from dicttoxml import dicttoxml\nimport xml.dom.minidom\n\nmy_dict = {\n    'name': 'Alice',\n    'age': 30,\n    'is_student': False,\n    'courses': [\n        {'title': 'Math', 'credits': 3},\n        {'title': 'Science', 'credits': 4}\n    ],\n    'address': {'street': '123 Main St', 'city': 'Anytown'}\n}\n\n# Convert dictionary to XML with default settings\nxml_bytes = dicttoxml(my_dict)\n\n# Pretty print the XML (optional)\ndom = xml.dom.minidom.parseString(xml_bytes)\npretty_xml_string = dom.toprettyxml()\n\nprint(pretty_xml_string)","lang":"python","description":"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."},"warnings":[{"fix":"Pass `attr_type=False` to the `dicttoxml` function: `dicttoxml(my_dict, attr_type=False)`.","message":"By default, `dicttoxml` includes a `type` attribute for each element (e.g., `<name type=\"str\">`). This can lead to verbose XML if not desired.","severity":"gotcha","affected_versions":"All versions (behavior since 1.4)."},{"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()`.","message":"The converted XML is wrapped in a `<root>` element by default, and includes an XML declaration (`<?xml version=\"1.0\" encoding=\"UTF-8\" ?>`).","severity":"gotcha","affected_versions":"All versions (behavior since 0.4 for root, 1.5 for custom_root)."},{"fix":"Sanitize dictionary keys to be valid XML element names before conversion, or be aware of `dicttoxml`'s automatic handling.","message":"Element 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\">`.","severity":"gotcha","affected_versions":"All versions."},{"fix":"Disable debugging output by calling `dicttoxml.set_debug(False)` or configure Python's `logging` module to suppress `dicttoxml`'s output.","message":"The library enables verbose logging by default, which can clutter console output or log files with INFO-level messages for every conversion step.","severity":"gotcha","affected_versions":"All versions (issue reported in 2017)."}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}