Next / Previous / Contents / Shipman's homepage

9.16. Element.iterdescendants(): Find all descendants

The term descendants refers to an element's children, their children, and so on all the way to the leaves of the document tree.

For an Element instance E, this method returns an iterator that visits all of E's descendants in document order.


If you want the iterator to visit only elements with a specific tag name N, pass an argument tag=N.


>>> xml = '''<root>
...   <grandpa>
...     <dad>
...       <yuo/>
...     </dad>
...   </grandpa>
... </root>'''
>>> root = etree.fromstring(xml)
>>> you = root.xpath('.//yuo')[0]
>>> for  anc in you.iterancestors():
...    print anc.tag,
dad grandpa root