Expat current line/column number

Nicolas Fleury nid_oizo at yahoo.com_remove_the_
Wed Oct 15 14:34:32 EDT 2003


Alan Kennedy wrote:

> [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?

It doesn't work on my side or I'm missing something.

Here's your example slightly modified:

import StringIO
import xml.sax
import xml.sax.xmlreader
import xml.sax.expatreader

class ApplicationException(Exception): pass

class HandlerThatRaisesExceptions(xml.sax.ContentHandler):
     def startElement(self, name, attrs):
         if name == 'hello':
             raise Exception('wrong')

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

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


My output is:
App exception:wrong: Line 15, Column 0

I don't understand these numbers; there's not even 15 lines...  Anyone 
have an idea what I'm missing?

Thx and Regards,

Nicolas Fleury





More information about the Python-list mailing list