Passing data out of a Sax parser

John J. Lee jjl at pobox.com
Sun Sep 21 23:51:55 CEST 2003


"Andrew Dalke" <adalke at mindspring.com> writes:

> 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

Works, but integers aren't mutable.


John




More information about the Python-list mailing list