explaingit

martinblech/xmltodict

5,735PythonAudience · developerComplexity · 1/5LicenseSetup · easy

TLDR

xmltodict is a tiny Python library that converts XML documents into Python dictionaries and back, making XML data as easy to navigate and modify as JSON with no extra dependencies.

Mindmap

mindmap
  root((xmltodict))
    What it does
      XML to dict
      Dict to XML
      Stream large files
    Conversion rules
      Elements as keys
      Attributes with prefix
      Repeated items as list
    Features
      Namespace support
      No extra dependencies
      Streaming mode
    Install
      pip install
      Pure Python
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Parse an XML API response into a Python dictionary so you can access nested fields and attributes like any other Python data.

USE CASE 2

Process a large XML data dump using streaming mode to avoid loading the entire file into memory.

USE CASE 3

Round-trip XML: read an XML config file, modify values as a Python dict, then write it back as valid XML.

USE CASE 4

Convert legacy SOAP or RSS XML responses into Python dictionaries for easier data processing.

Tech stack

Pythonpip

Getting it running

Difficulty · easy Time to first run · 5min
MIT, use freely for any purpose including commercial projects.

In plain English

xmltodict is a Python library that converts XML documents into Python dictionaries, making XML data easier to work with in code. XML is an older data format that uses angle-bracket tags to organize information, while a Python dictionary is a simpler key-value structure that most Python code already knows how to handle. The library bridges the two by parsing an XML document and producing a dictionary that mirrors its structure. The conversion follows a predictable set of rules. XML element names become dictionary keys. Repeated elements at the same level become a list. XML attributes are stored in the same dictionary with an @ prefix so they are easy to spot and separate from element content. Text content inside an element is stored under a key called #text. This consistent mapping means you can navigate the resulting dictionary the same way you would navigate any other Python data structure. The library also supports going the other direction. You can call its unparse function on a dictionary and get back a valid XML document. This makes it possible to read an XML file, modify the data as a dictionary, and write it back out as XML without using a heavier XML processing library. For large XML files, such as data dumps from Wikipedia or similar sources, the library includes a streaming mode. Instead of loading the entire file into memory at once, it calls a function you provide for each item at a specified depth in the document. This allows processing very large XML files without running out of memory. Installation is through pip. The library is small, written in pure Python, and has no required dependencies beyond the standard library's built-in XML parser. It supports XML namespaces, with options to expand them, collapse them to short prefixes, or ignore them entirely.

Copy-paste prompts

Prompt 1
Parse this XML API response with xmltodict in Python and show me how to access nested elements and XML attributes.
Prompt 2
Use xmltodict streaming mode to process a large Wikipedia XML dump and extract all article titles without running out of memory.
Prompt 3
Convert a Python dictionary back to XML using xmltodict.unparse() and write the result to a file.
Prompt 4
How does xmltodict handle repeated XML elements at the same level, does it produce a list or overwrite the key?
Open on GitHub → Explain another repo

← martinblech on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.