views:

27

answers:

1

Hi,

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.

A: 

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.

Robert Kluin
@Robert Kluin. Thanks for the quick reply.Xml which i had uploaded was a dummy.In my application,each element will have 'name' attribute.
abhishek
Thank You robert.
abhishek