views:

68

answers:

1

I am using the builtin Python ElementTree module. It is straightforward to access children, but what about parent nor sibling nodes? - can this be done efficiently without traversing the entire tree?

+2  A: 

There's no direct support in the form of a parent attribute, but you can perhaps use the patterns described here to achieve the desired effect. The following one-liner is suggested (from the linked-to post) to create a child-to-parent mapping for a whole tree:

parent_map = dict((c, p) for p in tree.getiterator() for c in p)
Vinay Sajip