Parsing XML using SAX

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


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'],
                        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'],

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list