Python Glossary Русский

all Python 3until 3.9Removed from the standard libraryfunction

xml.etree.ElementTree.Element.getiterator()

removed

Walked an XML element and all its descendants at any depth. Removed in Python 3.9; use elem.iter() instead.

Syntax

elem.getiterator(tag=None)  # до 3.9

The method returned a list of the element and every element nested inside it, optionally only those with a given tag. Python 2.7 and 3.2 added elem.iter(), which does the same lazily as an iterator, so getiterator() was deprecated and removed in 3.9 (on both Element and ElementTree). How to rewrite: elem.getiterator() becomes elem.iter(), elem.getiterator('item') becomes elem.iter('item'); if you really need a list, use list(elem.iter()).

Examples

# Python 3.9+
import xml.etree.ElementTree as ET
root = ET.fromstring('<r><a><b/></a></r>')
try:
    root.getiterator()
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.9+
import xml.etree.ElementTree as ET
root = ET.fromstring('<r><a><b/></a><b/></r>')
print([el.tag for el in root.iter()])  # → ['r', 'a', 'b', 'b']
print(len(list(root.iter('b'))))  # → 2
import xml.etree.ElementTree as ET
root = ET.fromstring('<r><a><b/></a><b/></r>')
print([el.tag for el in root.getiterator()])  # → ['r', 'a', 'b', 'b']

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy