将一个Etree XML结构转换为一个Python dict+list格式的代码
来源:http://code.activestate.com/recipes/578244-lxml-etree-xml-object-to-basic-python-dictlists/?in=lang-python
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
## end of http://code.activestate.com/recipes/578244/ }}}




