{"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":"python","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,"search_vec":"'1.7.8':30 'activ':20 'convers':32 'convert':10 'current':27 'design':8 'dict':34 'dict2xml':1,2 'dictionari':13,33 'frequent':23 'maintain':21 'minor':24 'python':6,12 'releas':25 'serial':35 'small':5 'string':17 'util':7 'version':28 'xml':16,31","created_at":"2026-04-12T03:35:00.594961+00:00","updated_at":"2026-04-16T06:21:15.672601+00:00","problems":[{"fix":"Install the library using pip: `pip install dict2xml`","cause":"The 'dict2xml' library is not installed in your Python environment or there is a typo in the import statement.","error":"ModuleNotFoundError: No module named 'dict2xml'"},{"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)`.","cause":"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.","error":"AttributeError: module 'dict2xml' has no attribute 'convert'"},{"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`.","cause":"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.","error":"TypeError: Elements with an unsupported data type raise a TypeError exception."},{"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.\n\n```python\nfrom dict2xml import dict2xml, Converter\nfrom collections import OrderedDict\n\ndata = OrderedDict([('b', 2), ('a', 1)])\nxml_string = dict2xml(data, data_sorter=False) # For the top-level function if it supports it\n# Or using the Converter class explicitly:\nconverter = Converter()\nxml_string = converter.build(data, data_sorter=Converter.DataSorter.never())\n```","cause":"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.","error":"Dictionary order not maintained in XML output"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.7.8","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"http://github.com/delfick/python-dict2xml","docs":null,"changelog":null,"pypi":"https://pypi.org/project/dict2xml/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}