Next / Previous / Contents / Shipman's homepage

9.15. Element.iterchildren(): Find all children

For an Element instance E, this method returns an iterator that iterates over all of E's children.

E.iterchildren(reversed=False, tag=None)

Normally, the resulting iterator will visit the children in document order. However, if you pass reversed=True, it will visit them in the opposite order.

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

Example:

>>> root=et.fromstring("<mom><aaron/><betty/><clarence/><dana/></mom>")
>>> for kid in root.getchildren():
...     print kid.tag
aaron
betty
clarence
dana
>>> for kid in root.iterchildren(reversed=True):
...     print kid.tag
... 
dana
clarence
betty
aaron
>>>