iterate over a series of nodes in an XML file

Steve M sjmaster at gmail.com
Wed Jul 5 18:14:21 EDT 2006


I see you've had success with elementtree, but in case you are still
thinking about SAX, here is an approach that might interest you. The
idea is basically to turn your program inside-out by writing a
standalone function to process one myID node. This function has nothing
to do with SAX or parsing the XML tree. This function becomes a
callback that you pass to your SAX handler to call on each node.

import xml.sax

def myID_callback(data):
    """Process the text of one myID node - boil it, mash it, stick it
in a list..."""
    print data

class MyHandler(xml.sax.ContentHandler):
    def __init__(self, myID_callback):
        #a buffer to collect text data that may or may not be needed
later
        self.current_text_data = []
        self.myID_callback = myID_callback

    def characters(self, data):
        """Accumulate characters. startElement("myID") resets it."""
        self.current_text_data.append(data)

    def startElement(self, name, attributes):
        if name == 'myID':
            self.current_text_data = []

    def endElement(self, name):
        if name == 'myID':
            data = "".join(self.current_text_data)
            self.myID_callback(data)

filename = 'idlist.xml'
xml.sax.parse(filename, MyHandler(myID_callback))




More information about the Python-list mailing list