[XML-SIG] Re: SAX parsing

Fredrik Lundh fredrik at pythonware.com
Sun Sep 19 14:29:02 CEST 2004

"Ajay" wrote:

> i have an xml document and would just like to get information placed in a
> certain tag. I am thinking of using SAX with a content handler which then
> handles the particular tag.
> the tag will most likely appear at the start of the document. Is it
> possible to stop the document parsing after i receive the tag? how would i
> do it?

here's one way to do it:

class Found(Exception):

class MyContentHandler(...):
    def startElement(self, name, attrs):
        if name == "some tag":
            raise Found("some information")


except Found, value:
    # found it
    print value

(this returns the value inside the exception instance.  it might be better
to attach it to some other object (such as the parser instance), but I'm
too lazy to figure out how to access the parser from inside the content


More information about the XML-SIG mailing list