[Pythonmac-SIG] XML handler design

Steve Spicklemire steve at spvi.com
Thu Mar 24 15:10:04 CET 2005


Hi Henning,

>
>>> def startElement(self, name, attrs):
>>>     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 self.__class__.__dict__[handler]?


how about:

		handler = getattr(self, str('_start_'+name),None)  # fetch the actual 
bound method
		if handler is not None:
			handler( attrs )

or something like that? I'm not sure why you're using the 
__class__.__dict__ attributes in the first place, so maybe what I 
suggest can't work.

>
> And where should the "output" go to?
> All examples use print statements in the element handlers.
> I wrote those get... methods - but I guess they don't belong in the 
> XML handler, but perhaps in the parser or somewhere else.
> It works, but I don't think it's good design.

I've not uesd the sax stuff myself.. but I'd say you can either save it 
in the handler, as you're doing, or pass some kind structure/object to 
the handler's constructor that it will use to save away the content it 
finds.

good luck!
-steve

>
>>> def getPages(self):
>>>     return self.pages.getSortedArray()
>>>
>>> def getPage(self, no):
>>>     return self.pages[no]
>
>>> parser = xml.sax.make_parser()
>>> parser.setFeature(xml.sax.handler.feature_namespaces, 0)
>>> pxh = MyHandler()
>>> parser.setContentHandler(pxh)
>>> parser.parse(dateiname)
>>> for p in pxh.getPages(): ...
>
> I should ask the last question on the twisted ML, I guess:
>
>>> 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.
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG at python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
>



More information about the Pythonmac-SIG mailing list