Expat current line/column number

Alan Kennedy alanmk at hotmail.com
Wed Oct 15 13:30:40 EDT 2003


[Nicolas Fleury]
>      Is it possible with xml.parsers.expat to get the current line and
> column number if an exception is raised by one of the callbacks (not
> necessary an xml.parsers.expat.ExpatError)?

Is this the type of thing you mean?

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import StringIO
import xml.sax
import xml.sax.xmlreader
import xml.sax.expatreader

class ApplicationException(Exception): pass

class HandlerThatRaisesExceptions(xml.sax.ContentHandler):

    def endDocument(self):
        raise ApplicationException('End-of-document Exception')

inputfile = StringIO.StringIO("""
<well>
    <formed/>
</well>
""")

parser = xml.sax.make_parser('xml.sax.expatreader')
parser.setContentHandler(HandlerThatRaisesExceptions())
try:
    parser.parse(inputfile)
except ApplicationException, ax:
    print "App exception:%s: Line %d, Column %d" % (str(ax), \
        parser.getLineNumber(), parser.getColumnNumber()) 

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list