Parsing XML using SAX
fredrik at pythonware.com
Mon Oct 23 16:58:22 CEST 2006
Nathan Harmston wrote:
> However it is quite messy at the moment. The main reason is that Python
> doesnt have a switch statement.
a switch statement wouldn't save you a single line, so I find it a bit hard
to believe that it's the main reason...
> 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.
forget about SAX and silly state machines, and use a modern XML library;
here's some ElementTree inspiration:
there are several other newer libraries that provide built-in data binding support,
including lxml.objectify and Amara.
More information about the Python-list