Skip to content

Commit

Permalink
Merge pull request realpython#311 from martinblech/master
Browse files Browse the repository at this point in the history
Add xmltodict to xml scenario.
  • Loading branch information
Kenneth Reitz committed Sep 16, 2013
2 parents e66ca84 + bbbb24d commit 6b53b2f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/scenarios/xml.rst
Expand Up @@ -32,3 +32,42 @@ and then you can get the child elements name like this:
untangle also supports loading XML from a string or an URL.

xmltodict
---------

`xmltodict <http://github.com/martinblech/xmltodict>`_ is another simple
library that aims at making xml feel like working with json.

An xml file like this:

.. code-block:: xml
<mydocument has="an attribute">
<and>
<many>elements</many>
<many>more elements</many>
</and>
<plus a="complex">
element as well
</plus>
</mydocument>
can be loaded into a python dict like this:

.. code-block:: python
import xmltodict
obj = xmltodict.parse('path/to/file.xml')
and then you can access elements, attributes and values like this:

.. code-block:: python
doc['mydocument']['@has'] # == u'an attribute'
doc['mydocument']['and']['many'] # == [u'elements', u'more elements']
doc['mydocument']['plus']['@a'] # == u'complex'
doc['mydocument']['plus']['#text'] # == u'element as well'
xmltodict also lets you roundtrip back to xml with the unparse function,
has a streaming mode suitable for handling files that don't fit in memory
and supports namespaces.

0 comments on commit 6b53b2f

Please sign in to comment.