[XML-SIG] 0.5.1 and 0.6.2

Ken MacLeod ken@bitsko.slc.ut.us
30 Nov 2000 18:09:07 -0600


Michael Sobolev <mss@transas.com> writes:

[Michael already pointed out he's using DOM, but I already had this
written in case anyone finds it useful.]

I have a SAX client that I've got working well with SAX1 and SAX2 so
far.  Having Unicode strings caught me in one place, and I needed to
wrap it with str() to make it work in that context, but I have no
general tips if that goes wrong for anyone.

In my SAX handler module, in the file global scope, I do this:

  import sys

  if hasattr(sys, 'version_info'):
      isPy2 = 1
      import xml.sax
      from xml.sax.handler import feature_namespaces
      from xml.sax import SAXException
  else:
      isPy2 = 0
      from xml.sax import saxexts
      from xml.sax.saxlib import SAXException

When creating the parser I do this:

        if isPy2:
            self.parser = xml.sax.make_parser()
            self.parser.setFeature(feature_namespaces, 0)
            self.parser.setContentHandler(self)
        else:
            self.parser = saxexts.make_parser()
            self.parser.setDocumentHandler(self)

I'm parsing files, so later I do:

        if isPy2:
            self.parser.parse(file)
        else:
            self.parser.parseFile(file)

While working with attributes in startElement(), I do this to get a
list of attribute names to use as indexes into the attributes:

        if isPy2:
            att_names = atts.keys()
        else:
            att_names = []
            for ii in range(0, len(atts)):
                att_names.append(atts[ii])

And for characters(), I do this:

    def characters(self, ch, start=0, length=-1):
        if length == -1:   # SAX2
            self.text = self.text + ch
        else:
            self.text = self.text + ch[start:start+length]

I do my own namespace processing (more for convenience than for
SAX1/SAX2 differences), so that makes start/endElement() usable for
both SAX1 and SAX2.  Otherwise you'll need both start/endElement() and
start/endElementNS().  If you do use namespace processing, you need no
special code in startElement() (as above) because you know only
startElement() will be called from SAX1 and startElementNS() will be
called from SAX2.

  -- Ken