Parsing XML using SAX

Nathan Harmston ratchetgrid at googlemail.com
Mon Oct 23 16:34:10 CEST 2006


Hi,

Currently I m writing an XML parser that processes an xml file using sax,
and I have it working, however I want to make the code of my parser less
cluttered and more readable by other people (including myself). However it
is quite messy at the moment. The main reason is that Python doesnt have a
switch statement.

 def startElement(self,name,attributes):
                if name == "sbml":
                        s = Sbml(attributes['xmlns'], attributes['version'],
attributes['level'])
                        self.sbmlDict['sbml'] = s
                elif name == "model":
                        m = Model(attributes['id'], attributes['name'])
                        self.sbmlDict['model'] = m
                elif name == "listOfCompartments":
                        self.inListOfCompartments = bool(1)
                elif name == "compartment" and self.inListOfCompartments:
                        c = Compartment(attributes['id'],
attributes['name'])
                        self.tempList.append(c)
.......................................snip

I would use a dictionary for this, but this would require the use of many
extra methods for each tag name, and this would lead to clutter aswell. Does
anyone have any suggestions for reducing the number of lines and making my
code look neater than a large amount of methods or elif statements.

Many Thanks

Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061023/d24d5318/attachment.html>


More information about the Python-list mailing list