简体   繁体   中英

Read dynamic xml using element tree

Environment: Windows,Python,wxpython and Element tree as xml parser.

I am developing a stand alone where it reads the xml and creates a tree. My application reads the xml and creates tree but when xml changes next time(when DEPTH of xml increases- i mean when two child elements are added).Application fails to read(Logic fails :( )

For eg I have written a logic which can read any xml which has a depth of 5.But when it reads an xml with a depth more than 5 , it fails. Please let me know how to read xml whose depth is dynamic.

You should use recursive calls, something more like:

def recurse_tree(node):
    tree = {}
    for element in node:
        name = element.get('name')
        tree[name] = recurse_tree(element)
    if tree:
        return tree
    else:
        return 'No children.'

Not all of your elements had 'name' attributes. So you will need to adjust this to match your exact data structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM