Skip to content

Commit bbbb24d

Browse files
committed
Add xmltodict
1 parent 036c803 commit bbbb24d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/scenarios/xml.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,42 @@ and then you can get the child elements name like this:
3232
3333
untangle also supports loading XML from a string or an URL.
3434

35+
xmltodict
36+
---------
37+
38+
`xmltodict <http://github.com/martinblech/xmltodict>`_ is another simple
39+
library that aims at making xml feel like working with json.
40+
41+
An xml file like this:
42+
43+
.. code-block:: xml
44+
45+
<mydocument has="an attribute">
46+
<and>
47+
<many>elements</many>
48+
<many>more elements</many>
49+
</and>
50+
<plus a="complex">
51+
element as well
52+
</plus>
53+
</mydocument>
54+
55+
can be loaded into a python dict like this:
56+
57+
.. code-block:: python
58+
59+
import xmltodict
60+
obj = xmltodict.parse('path/to/file.xml')
61+
62+
and then you can access elements, attributes and values like this:
63+
64+
.. code-block:: python
65+
66+
doc['mydocument']['@has'] # == u'an attribute'
67+
doc['mydocument']['and']['many'] # == [u'elements', u'more elements']
68+
doc['mydocument']['plus']['@a'] # == u'complex'
69+
doc['mydocument']['plus']['#text'] # == u'element as well'
70+
71+
xmltodict also lets you roundtrip back to xml with the unparse function,
72+
has a streaming mode suitable for handling files that don't fit in memory
73+
and supports namespaces.

0 commit comments

Comments
 (0)