[XML-SIG] how to clean up parser without causing parsing?

Div Shekhar div@commerceflow.com
Fri, 22 Dec 2000 17:50:20 -0800


Hi!

I'm using xmlproc through the SAX interface. (PyXML 0.5.5.1/Python 1.6)
I have this code to parse a file:

! p = XMLParserFactory.make_parser( 'xml.sax.drivers.drv_xmlproc' )
! sp = MyHandler()
! p.setDocumentHandler( sp )   # other handlers left out for simplicity
! try:
!     p.parseFile( file )
! finally:                     # even if an exception is raised
!     p.close()                #  call close() to free memory

My handler does some validation, and raises an exception when it's not
happy with the XML that comes from the file.

The close() causes remaining data to be parsed, which results in more
SAX callbacks coming to my handler, which throws new exceptions which
are very confusing.

To work around this, I replaced the 'p.close()' with the close()
implementation in drv_xmlproc, and moved one line above the 'finally':

! try:
!     p.parseFile( file )
!     p.parser.close()                      # \   cut & paste from
! finally:
!     p.parser.deref()                      #  |  drv_xmlproc.close()
!     p.err_handler = p.dtd_handler = None  #  |
!     p.doc_handler = p.parser = None       #  |  
!     p.locator = p.ent_handler = None      # /

I thought of the following alternatives:

1. have my handler set a flag, and then ignore further calls.
2. point the parser to a do nothing handler before calling close()
3. doing the following:  p.reset()  p.close()

But they're not as efficient. What should I be doing?

Sincerely,
Div

(P.S. Any chance of ExtendedParser adding a free() method? :)