I have a problem with ElementTree.iter().
So I tried this example in this link : http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/
So here's what I've tried:
import elementtree.ElementTree as ET
tree = ET.parse('XML_file.xml')
root = tree.getroot()
for elem in tree.iter():
print elem.tag, elem.attrib
And I get this error AttributeError: ElementTree instance has no attribute 'iter'
Additional info: The version of my Python is 2.4 I separately installed elementtree. Other examples in the link that I provide is working in my Python installed. Only the ElementTree.iter() is not working. Thanks in advance for all of your help. Cheers!
In your case, you should replace the
.iter()
by.getiterator()
, and you possibly should call it for theroot
element, not for the tree (but I am not sure because I do not have the Python 2.4 and the module at my hands).This is the older functionality that was deprecated in Python 2.7. For Python 2.7, the
.iter()
should work with the built-in module:A side note: the standard module supports also direct iteration through the element node (i.e. no
.iter()
or whatever method called, just thefor elem in root:
). It differs from.iter()
-- it goes only through the immediate descendant nodes. Similar functionality is implemented in the older versions as.getchildren()
.Try to use findall instead of iter. ElementTree's iter() equivalent in Python2.6
According to python document this API should be in 2.5, however it doesn't exist. You can use the below mentioned code for iteration. This way you can also pass the tag.