Convert an Etree XML structure into a Python dict+list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from lxml import etree, objectify
def formatXML(parent):
"""
Recursive operation which returns a tree formated
as dicts and lists.
Decision to add a list is to find the 'List' word
in the actual parent tag.
"""
ret = {}
if parent.items(): ret.update(dict(parent.items()))
if parent.text: ret['__content__'] = parent.text
if ('List' in parent.tag):
ret['__list__'] = []
for element in parent:
if element.tag is not etree.Comment:
ret['__list__'].append(formatXML(element))
else:
for element in parent:
if element.tag is not etree.Comment:
ret[element.tag] = formatXML(element)
return ret
|
More info at: http://www.luismartingil.com/2012/08/14/etree-xml-to-python-dictlists/