xmltodict converts XML to nested Python dicts and back, with optional namespaces, streaming for large files, and CLI piping support.
Overview
xmltodict parses XML into structures that mirror JSON workflows, using configurable keys for text, attributes, and comments. Namespace expansion, prefix collapsing, and selective skipping are available when processnamespaces is enabled.
An Expat-based streaming mode limits memory use on large feeds such as Wikipedia or Discogs dumps. The unparse() path rebuilds XML from dictionaries, with options for pretty printing, empty elements, and nested list tags.
API options cover security-oriented entity handling, postprocessors, forced lists, CDATA controls, and whitespace preservation trade-offs documented for round-tripping limits.
Distribution packages are noted for PyPI, conda-forge, and several Linux and BSD distributions.
Key features
- XML to dict and dict to XML
- Optional namespace processing
- Streaming parse mode
- Command-line pipe integration
- Configurable attribute and text keys
- Selective list forcing
- Optional comment preservation
- Security-focused entity defaults
Use cases
- Consuming XML APIs as dicts
- Batch processing large XML dumps
- Lightweight config file conversion
- Shell pipelines through XML data
- Round-tripping simple XML documents