开发者

Read dynamic xml using element tree

开发者 https://www.devze.com 2023-01-21 13:14 出处:网络
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.

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 e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消