Welcome, guest | Sign In | My Account | Store | Cart

Convert an Etree XML structure into a Python dict+list

Python, 22 lines
 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

1 comment