In many applications, you will want to visit every
element in a document, or perhaps to retrieve information
from all the tags of a certain kind. This method, on
some ElementTree instance , will return an
iterator that visits all matching tags.
ET
ET.getiterator(tag=None)
If you omit the argument, you will get an iterator that generates every element in the tree, in document order.
If you want to visit only tags with a certain name, pass that name as the argument.
Here are some examples. In these examples, assume that
page is an ElementTree
instance that contains an XHTML page. The first example
would print every tag name in the page, in document order.
for elt in page.getiterator():
print elt.tag
The second example would look at every div
element in the page, and for those that have a class attribute, it prints those attributes.
for elt in page.getiterator('div'):
if elt.attrib.has_key('class'):
print elt.get('class')