xmltodict
raw JSON → 1.0.4 verified Tue May 12 auth: no python install: verified quickstart: verified
xmltodict makes working with XML feel like you are working with JSON. The current version is 1.0.4, with regular updates and bug fixes.
pip install xmltodict Common errors
error ModuleNotFoundError: No module named 'xmltodict' ↓
cause The xmltodict library is not installed in the Python environment where the script is being run.
fix
pip install xmltodict
error xml.parsers.expat.ExpatError: syntax error: line X, column Y ↓
cause The input XML string is not well-formed, contains syntax errors, or invalid characters, preventing xmltodict from parsing it correctly.
fix
Ensure the XML string is syntactically correct and well-formed according to XML standards, or handle potential parsing errors with a try-except block.
error KeyError: 'some_element_name' ↓
cause An attempt was made to access an XML element or attribute using a dictionary key that does not exist in the parsed output, often due to incorrect case, wrong path, or the element being an attribute (which are prefixed with '@').
fix
Inspect the structure of the parsed dictionary output (e.g., by printing it) to identify the correct key, paying attention to case sensitivity and attribute prefixes like '@attribute_name'.
error ValueError: Document must have exactly one root. ↓
cause The dictionary provided to `xmltodict.unparse` does not represent a valid XML structure because it lacks a single top-level key that would serve as the XML root element, or it has multiple top-level keys.
fix
Ensure the input dictionary for unparsing has exactly one top-level key that will act as the root element of the generated XML document.
error xmltodict.expat.ExpatError: not well-formed (invalid token): line 1, column 0 ↓
cause The input provided to `xmltodict.parse()` is not valid XML or contains malformed characters, preventing proper parsing.
fix
Validate the XML input for well-formedness and correct any syntax errors, mismatched tags, or invalid characters before passing it to
xmltodict.parse(). Warnings
breaking Version 1.0.0 dropped legacy compatibility paths for Python 2.x. ↓
fix Upgrade to Python 3.9+ and use version 1.0.0 or higher.
gotcha Avoid using outdated patterns to import the library. ↓
fix Use 'import xmltodict' instead of legacy import patterns.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.12s 17.8M
3.10 slim (glibc) - - 0.08s 18M
3.11 alpine (musl) - - 0.17s 19.7M
3.11 slim (glibc) - - 0.14s 20M
3.12 alpine (musl) - - 0.16s 11.6M
3.12 slim (glibc) - - 0.15s 12M
3.13 alpine (musl) - - 0.14s 11.2M
3.13 slim (glibc) - - 0.14s 12M
3.9 alpine (musl) - - 0.12s 17.3M
3.9 slim (glibc) - - 0.10s 18M
Imports
- xmltodict
import xmltodict
Quickstart verified last tested: 2026-04-23
import xmltodict
xml_str = '<foo><bar value="1">test</bar></foo>'
dict_obj = xmltodict.parse(xml_str)
print(dict_obj)