Typing stubs for xmltodict
This package provides static type annotations for the `xmltodict` library, enabling type checkers like MyPy, Pyright, or PyCharm to validate code that uses `xmltodict`. It is part of the `typeshed` project, a community-maintained repository for Python library stubs, and is automatically released to PyPI. The current version is 1.0.1.20260408, with `typeshed` typically releasing updates daily.
Warnings
- breaking The `xmltodict` library (which `types-xmltodict` stubs) introduced breaking changes in version 1.0.0, primarily by modernizing for Python 3.9+ and dropping legacy compatibility paths. Ensure your `xmltodict` runtime version matches the expectations of the `types-xmltodict` stubs (e.g., `xmltodict~=1.0.1` for `types-xmltodict==1.0.1.x`).
- gotcha `types-xmltodict` is a stub-only package and does not contain any runtime code. It provides only type annotations for static analysis. You must install the `xmltodict` runtime library separately for your Python code to execute successfully.
- gotcha The version numbering for `types-xmltodict` (e.g., `1.0.1.20260408`) indicates that it targets `xmltodict~=1.0.1`. Using a `types-xmltodict` version that is incompatible with your installed `xmltodict` runtime version (e.g., `types-xmltodict` for `1.0.x` with `xmltodict` `0.15.x`) can lead to incorrect or misleading type checking results.
- gotcha `xmltodict` prioritizes convenience for common XML-to-JSON-like conversions and does not guarantee perfect round-tripping of every XML nuance, such as attribute order, mixed content ordering, or the exact order of multiple top-level comments. For exact fidelity, consider a full XML library like `lxml`.
- gotcha A disputed CVE (CVE-2025-9375) was filed against `xmltodict` concerning potential XML injection due to underlying limitations in Python's `xml.sax.saxutils.XMLGenerator`. While `xmltodict` has added internal validations, users processing untrusted XML inputs should be aware of these security considerations.
- gotcha When using `xmltodict.unparse()` to convert a Python dictionary back to XML, the input dictionary must contain a single root element. If the dictionary represents multiple top-level XML elements, `unparse` will raise a `ValueError`.
- gotcha `xmltodict`'s default behavior for elements that might appear once or multiple times is inconsistent: a single occurrence might be a direct value, while multiple occurrences become a list. The `force_list` parameter (e.g., `xmltodict.parse(xml_input, force_list=('elements',))`) is crucial for consistent handling, especially with deeply nested structures.
Install
-
pip install types-xmltodict xmltodict
Imports
- parse
import xmltodict xmltodict.parse(...)
- unparse
import xmltodict xmltodict.unparse(...)
Quickstart
import xmltodict
# Example XML string
xml_string = """
<person>
<name>John Doe</name>
<age>30</age>
<city>New York</city>
</person>
"""
# Parse XML to a Python dictionary (type-checked by types-xmltodict)
data_dict = xmltodict.parse(xml_string)
print(f"Parsed Dictionary: {data_dict}")
# Access data (type-checked)
name = data_dict['person']['name']
print(f"Name: {name}")
# Modify data
data_dict['person']['age'] = 31
# Unparse dictionary back to XML (type-checked)
modified_xml = xmltodict.unparse(data_dict, pretty=True, indent=' ')
print(f"\nModified XML:\n{modified_xml}")