Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Makes working with XML feel like you are working with JSON

Project description

# xmltodict

`xmltodict` is a Python module that makes working with XML feel like you are working with [JSON](, as in this ["spec"](

[![Build Status](](

>>> doc = xmltodict.parse("""
... <mydocument has="an attribute">
... <and>
... <many>elements</many>
... <many>more elements</many>
... </and>
... <plus a="complex">
... element as well
... </plus>
... </mydocument>
... """)
>>> doc['mydocument']['@has']
u'an attribute'
>>> doc['mydocument']['and']['many']
[u'elements', u'more elements']
>>> doc['mydocument']['plus']['@a']
>>> doc['mydocument']['plus']['#text']
u'element as well'

It's very fast ([Expat]( and has a streaming mode with a small memory footprint, suitable for big XML dumps like [Discogs]( or [Wikipedia](

>>> def handle_artist(_, artist):
... print artist['name']
>>> xmltodict.parse(GzipFile('discogs_artists.xml.gz'),
... item_depth=2, item_callback=handle_artist)
A Perfect Circle
King Crimson
Chris Potter

It can also be used from the command line to pipe objects to a script like this:

import sys, marshal
while True:
_, article = marshal.load(sys.stdin)
print article['title']

$ cat enwiki-pages-articles.xml.bz2 | bunzip2 | 2 |

Or just cache the dicts so you don't have to parse that big XML file again. You do this only once:

$ cat enwiki-pages-articles.xml.bz2 | bunzip2 | 2 | gzip > enwiki.dicts.gz

And you reuse the dicts with every script that needs them:

$ cat enwiki.dicts.gz | gunzip |
$ cat enwiki.dicts.gz | gunzip |

## Ok, how do I get it?

You just need to

$ pip install xmltodict

There is an [official Fedora package for xmltodict]( If you are on Fedora or RHEL, you can do:

$ sudo yum install python-xmltodict

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for xmltodict, version 0.4.6
Filename, size File type Python version Upload date Hashes
Filename, size xmltodict-0.4.6.tar.gz (14.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page