Next / Previous / Contents / TCC Help System / NM Tech 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.

E.iterdescendants(tag=None)

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

Example:

>>> 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
>>>