Passing data out of a Sax parser
Tim Rowe
tim at remove_if_not_spam.digitig.co.uk
Mon Sep 22 10:36:48 EDT 2003
On Sat, 20 Sep 2003 00:53:06 GMT, "Andrew Dalke"
<adalke at mindspring.com> wrote:
>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
Ah! Of course! Thanks, I should have thought of that. As others
have pointed out, startDocument can create any sort of an object
there, not just a mutable, of course; it was my solution that required
a mutable.
More information about the Python-list
mailing list