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.
精彩评论