[XML-SIG] SAX_expat extension -- parseString()

Paul Schreiber paul@auctionflow.com
Mon, 24 Apr 2000 20:48:41 -0700


I hacked up my copy of drv_pyexpat.py because I wanted to be able to
parse XML that didn't exist in files or URLs.

Paul


Here's the code:

class SAX_expat(saxlib.Parser,saxlib.Locator):
    "SAX driver for the Pyexpat C module."
    
    [snip]

    def parse(self,sysID):
        self.sysID=sysID
        self.parseString(urllib.urlopen(sysID).read())

    def parseFile(self,fileobj):
        self.parseString(fileobj.read())

    def parseString(self, str):
        self.reset()
        self.doc_handler.startDocument()

        if not self.parser.Parse( str, 1):
            self.__report_error()

        self.doc_handler.endDocument()


Here's a context diff:
*** /tmp/PyXML-0.5.3/xml/sax/drivers/drv_pyexpat.py     Tue Dec 15
18:54:33 1998
--- drv_pyexpat.py      Mon Apr 24 20:45:21 2000
***************
*** 53,77 ****
  
      def parse(self,sysID):
          self.sysID=sysID
!         self.parseFile(urllib.urlopen(sysID))
!         
      def parseFile(self,fileobj):
          self.reset()
          self.doc_handler.startDocument()
  
! #        while 1:
! #            buf=fileobj.read(16384)
! #
! #            if buf:
! #               if not self.parser.Parse(buf):
! #                    self.__report_error()
! #                    return
! #           else:
! #               break
! 
!         if not self.parser.Parse(fileobj.read(),1):
              self.__report_error()
!                             
          self.doc_handler.endDocument()
  
      # --- Locator methods. Only usable after errors.
--- 53,70 ----
  
      def parse(self,sysID):
          self.sysID=sysID
!         self.parseString(urllib.urlopen(sysID).read())
! 
      def parseFile(self,fileobj):
+         self.parseString(fileobj.read())
+ 
+     def parseString(self, str):
          self.reset()
          self.doc_handler.startDocument()
  
!         if not self.parser.Parse( str, 1):
              self.__report_error()
! 
          self.doc_handler.endDocument()
  
      # --- Locator methods. Only usable after errors.