Skip to content

Add xmltodict to xml scenario. #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/scenarios/xml.rst
Original file line number Diff line number Diff line change
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.