Passing data out of a Sax parser

Andrew Dalke adalke at mindspring.com
Fri Sep 19 20:53:06 EDT 2003


Tim Rowe:
> b. Add a parameter to the handler's __init__ method, that takes some
> sort of mutable object, and put the answer into that object.

Either that or have startDocument create the mutable object, as

class MyHandler(xml.sax.handlers.ContentHandler):
  def startDocument(self):
    self.count = 0
  def startElement(self, name, attrs):
    if name == "spam":
        self.count += 1

parser = xml.sax.make_parser()
h = MyHandler()
parser.setContentHandler(h)
h.parse(input)
print h.count

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list