[XML-SIG] RAX

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Thu, 4 May 2000 01:17:16 +0200


Lars Marius Garshol <larsga@garshol.priv.no> wrote:
> I'm not sure how we would actually implement this. The only XML parser
> we have that supports a pull-style interface is RXP, and I'm not sure
> if we can convert the other interfaces to pull-style interfaces in a
> sensible way (at least not on a level as low as SAX) without storing
> the entire sequence of events.

assuming that a pull-style parser is what I think it is, here's
how to convert any incremental parser (xmllib, sgmlop, expat,
etc) to a pull-style parser:

import xmllib

START, DATA, END =3D "start", "data", "end"

class XMLPuller(xmllib.XMLParser):

    def __init__(self, stream):
        xmllib.XMLParser.__init__(self)
        self.__stream =3D stream
        self.__tokens =3D []

    def get(self):
        while not self.__tokens:
            data =3D self.__stream.read(10000)
            if not data:
                self.close()
                break
            self.feed(data)
        if self.__tokens:
            return self.__tokens.pop(0)
        return None # end of stream

    def unknown_starttag(self, tag, attr):
        self.__tokens.append(START, tag, attr)

    def handle_data(self, data):
        self.__tokens.append(DATA, data)

    def unknown_endtag(self, tag):
        self.__tokens.append(END, tag)

puller =3D XMLPuller(open("myfile.xml"))

while 1:
    next =3D puller.get()
    if not next:
        break
    print next

</F>