[XML-SIG] Reading characters

Thomas B. Passin tpassin@comcast.net
Tue, 21 May 2002 19:49:34 -0400


[Albert Chin]

> > What do you mean by "read through"? Can you share a bit of code?
>
>   class read_pkg_db (saxlib.HandlerBase):
>     def __init__ (self, data, extract = 0):
>       self.in_data = 0
>       self.data = data
>       self.extract = extract      # whether or not to extract payload
>
>     ...
>
>     def startElement (self, name, attrs):
>
>     ...
>
>     def characters (self, ch, start, length):
>       if self.in_data and self.extract:
>         self.payload = self.payload + ch[start:start+length]
>

If there are many "ch" chunks to add to the payload, that might be the
slowest part of the chain, especially if the payload gets large.  There are
two standard ways to speed such string concatenations up.  You either use
CStringIO and get the data from it when you are done adding, or you append
each chunk to a list, then do list.join() at the end.  Either one can give
dramatic speedups if that's your problem.

I'd certainly try one of these first.

Cheers,

Tom P