Iterparse and ElementTree confusion

paul.sherwood at gmail.com paul.sherwood at gmail.com
Wed Aug 17 08:00:52 EDT 2005


Further to the above....

I pass my found element 'Product' to ElementTree's handy print all
function

def print_all(element):
    root = element
    #Create an iterator
    iter = root.getiterator()
    #Iterate
    for element in iter:
        #First the element tag name
        print "Element:", element.tag
        #Next the attributes (available on the instance itself using
        #the Python dictionary protocol
        if element.keys():
            print "\tAttributes:"
            for name, value in element.items():
                print "\t\tName: '%s', Value: '%s'"%(name, value)
        #Next the child elements and text
        print "\tChildren:"
        #Text that precedes all child elements (may be None)
        if element.text:
            text = element.text
            text = len(text) > 40 and text[:40] + "..." or text
            print "\t\tText:", repr(text)
        if element.getchildren():
            #Can also use: "for child in element.getchildren():"
            for child in element:
                #Child element tag name
                print "\t\tElement", child.tag
                #The "tail" on each child element consists of the text
                #that comes after it in the parent element content, but
                #before its next sibling.
                if child.tail:
                    text = child.tail
                    text = len(text) > 40 and text[:40] + "..." or text
                    print "\t\tText:", repr(text)

if the element i pass to the above is from

root = ElementTree(file='somefile.xml')
iter = root.getiterator()
    #Iterate
for element in iter:
        if element.tag == 'Product':
            print_all(element)

I print all of my required element's attributes, children, children's
children, etc

However if i pass the element i get from iterparse

    for event, elem in iterparse(filename):
        if elem.tag == "Products":
            print_all(elem)
        else:
            elem.clear()

i only get the attributes for <product> and a list of its children, no
further iteration into the children and the children's children

what am i missing?




More information about the Python-list mailing list