In your case, you should replace .iter() with .getiterator() , and you should probably call it for the root element, not the tree (but I'm not sure, because I don't have Python 2.4 and I have a module in hands).
import elementtree.ElementTree as ET tree = ET.parse('XML_file.xml') root = tree.getroot() for elem in root.getiterator(): print elem.tag, elem.attrib
This is older functionality, deprecated in Python 2.7. For Python 2.7, .iter() should work with the built-in module:
import xml.etree.ElementTree as ET tree = ET.parse('XML_file.xml') root = tree.getroot() for elem in root.iter(): print elem.tag, elem.attrib
Note: the standard module also supports direct iteration through the node element (i.e. no .iter() or any other method, only for elem in root: . It differs from .iter() - it goes only through the nodes of the immediate descendant. Similar functionality is implemented in older versions as .getchildren() .
source share