Work with XML in Python as if it were JSON: parse it into dicts and back
open sourceMITfree
xmltodict is a Python module by Martin Blech that turns XML into nested dictionaries, so working with it feels like working with JSON: attributes become @-prefixed keys, text becomes #text, and repeated elements become lists. unparse() turns a dictionary back into XML. It's built on the Expat parser, has a streaming mode with a small memory footprint for large documents, handles namespaces, and can force chosen elements to lists or CDATA. Entity parsing is off by default.
Problems it solves
- read an XML API response in Python as a dict instead of walking a DOM
- convert XML to JSON in a Python script
- process a large XML file in Python without loading it all into memory
Features
- XML to nested dicts and back (parse and unparse)
- streaming mode for large documents
- namespace support
- force_list and force_cdata for chosen elements
- entity parsing off by default
Alternatives
The same job
These do what xmltodict does.
- lxml XML and HTML processing for Python
