{"id":3960,"library":"dict2xml","title":"dict2xml","description":"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.","status":"active","version":"1.7.8","language":"en","source_language":"en","source_url":"https://github.com/delfick/python-dict2xml","tags":["xml","conversion","dictionary","dict","serializer"],"install":[{"cmd":"pip install dict2xml","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary function to convert a dictionary to XML.","symbol":"dict2xml","correct":"from dict2xml import dict2xml"}],"quickstart":{"code":"from dict2xml import dict2xml\n\ndata = {\n    'root_element': {\n        'item1': 123,\n        'item2': [\n            {'sub_item_a': 'hello'},\n            {'sub_item_b': 'world'}\n        ],\n        'item3': {\n            'nested_value': 'example'\n        }\n    }\n}\n\nxml_string = dict2xml(data, wrap='root', indent='  ')\nprint(xml_string)","lang":"python","description":"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."},"warnings":[{"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.","message":"The library does not support adding attributes directly to XML elements. Dictionary keys are treated as element names, and their values as element content.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"By 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.","severity":"gotcha","affected_versions":"All versions up to 1.7.8"},{"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).","message":"As 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).","severity":"breaking","affected_versions":"1.7.0 and later"},{"fix":"If an XML declaration is required, you must prepend it manually to the output string.","message":"The library does not automatically include an XML declaration line (e.g., `<?xml version=\"1.0\" encoding=\"UTF-8\"?>`) in the generated XML string.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}