XML parsing

https://farm3.staticflickr.com/2808/33888714601_a1f7d020a2_k_d.jpg

untangle

untangle is a simple library whichtakes an XML document and returns a Python object which mirrors the nodes andattributes in its structure.

For example, an XML file like this:

<?xml version="1.0"?><root><childname="child1"></root>

can be loaded like this:

importuntangleobj=untangle.parse('path/to/file.xml')

and then you can get the child elements name like this:

obj.root.child['name']

untangle also supports loading XML from a string or an URL.

xmltodict

xmltodict is another simplelibrary that aims at making XML feel like working with JSON.

An XML file like this:

<mydocumenthas="an attribute"><and><many>elements</many><many>more elements</many></and><plusa="complex">    element as well</plus></mydocument>

can be loaded into a Python dict like this:

importxmltodictwithopen('path/to/file.xml')asfd:doc=xmltodict.parse(fd.read())

and then you can access elements, attributes and values like this:

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 memoryand supports namespaces.