[Pythonmac-SIG] XML handler design

Henning.Ramm at mediapro-gmbh.de Henning.Ramm at mediapro-gmbh.de
Thu Mar 24 12:10:54 CET 2005

Hi there!

I just wrote a SAX handler for XML files that describe a newspaper issue (list of pages etc.); I'd like to know if I could do it better.

def startElement(self, name, attrs):
    """call an own handler method named _start_Something 
    for a Something element if it exists,
    to avoid a long list of 'if name== ...'"""
    self._queue.append(name) # keep the order of processed tags
    handler = str('_start_'+name)
    if hasattr(self, handler):
        self.__class__.__dict__[handler](self, attrs)
        # is there a better syntax for this?

The different element handlers fill an array of custom Page objects (self.pages).
One gets this array either directly as MyHandler.pages or via

def getPages(self):
    return self.pages.getSortedArray() # no, it's not a plain array ;-)
def getPage(self, no):
    return self.pages[no]

Is that the right way to get data from a handler? Like in:

parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
pxh = MyHandler()
for p in pxh.getPages(): ...

I guess it would be better (more logical) to get the data from the parser?

Further, if I'd like to use it in a twisted driven asynchronous app, would I let the parser run in a thread? (Or how can I make the parser non-blocking?)

Best regards,
Henning Hraban Ramm
Südkurier Medienhaus / MediaPro
Support/Admin/Development Dept.

More information about the Pythonmac-SIG mailing list