ElementTree's iter() equivalent in Python2.6

prosseek picture prosseek · Oct 1, 2011 · Viewed 18.3k times · Source

I have this code with ElementTree that works well with Python 2.7. I needed to get all the nodes with the name "A" under "X/Y" node.

from xml.etree.ElementTree import ElementTree

verboseNode = topNode.find("X/Y")
nodes = list(verboseNode.iter("A"))

However, when I tried to run it with Python 2.6, I got this error.

ionCalculateSkewConstraint.py", line 303, in getNodesWithAttribute
    nodes = list(startNode.iter(nodeName))
AttributeError: _ElementInterface instance has no attribute 'iter'

It looks like that Python 2.6 ElementTree's node doesn't have the iter(). How can I implement the iter() with Python 2.6?

Answer

Adam Wagner picture Adam Wagner · Oct 1, 2011

Not sure if this is what you are looking for, as iter() appears to be around in 2.6, but there's getiterator()

http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.getiterator