Processing XML in Python with ElementTree

25,778

Solution 1

In your case, you should replace the .iter() by .getiterator(), and you possibly should call it for the root 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).

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 the older functionality that was deprecated in Python 2.7. For Python 2.7, the .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

A side note: the standard module supports also direct iteration through the element node (i.e. no .iter() or whatever method called, just the for 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().

Solution 2

Try to use findall instead of iter. ElementTree's iter() equivalent in Python2.6

Share:
25,778
neo
Author by

neo

Updated on March 18, 2020

Comments

  • neo
    neo about 4 years

    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!

  • neo
    neo almost 12 years
    okay thanks for this, I will try this when I get home, I'll update you as soon as I tried that. thanks again!
  • neo
    neo almost 12 years
    I've tried the getiterator(), but is calls all the child of the root folder. How can I call a subelement of a child?
  • pepr
    pepr almost 12 years
    Yes, .iter() and .getiterator() are defined this way. The .getchildren() returns a list of only the children of the node (no grandchildren). You may be interested in lxml (lxml.de) that is avaliable also for Python 2.4, has the same API but implements also XPath 1.0 expressions.
  • neo
    neo almost 12 years
    I am now using .find() for looking for specific subelement, is this a good idea? Or there's another way of calling an specifi element? thanks again!.
  • pepr
    pepr almost 12 years
    It depends on what you need to find. The .find(pattern) finds the first element with the wanted properties. The pattern can be a tag or XPath expression. Similarly, .getiterator(tag) iterates through all elements with the given tag name. Have a look at getpython3.com/diveintopython3/xml.html to get some basic feel how to work with XPath, ElementTree, or lxml.