Python and XML Help
Diez B. Roggisch
deets at nospam.web.de
Sun Apr 12 03:51:53 EDT 2009
ookrin schrieb:
> I'm in the process of learning python and PyQt4. I had decided to make
> myself a simple app and soon discovered that I needed to crash into
> xml to use some of the data I was going to be getting off of the
> server.
>
> I picked up enough xml to use the sax parser to get the data out of
> the xml. I can get as far as printing it to the screen, but there is
> where I get stuck.... I can't seem to send the data to anywhere else,
> into another variable, to another function. The parser stops after the
> first line it finds.
>
> class offlineLoad():
> def loadXmlFile(self):
> print "Loading from File"
> xf = open('CharacterID.xml','r')
> xml = xmlHandler()
> saxparser = make_parser()
> print "parser created"
> saxparser.setContentHandler(xml)
> print "parser runn"
> try:
> saxparser.parse(xf)
> except:
> print "Error Reading xml"
This is a very bad thing to do - there are only very few justified cases
of catch-all for exceptions. This certainly isn't one, as it suppresses
the real cause for what is happening.
Which makes it harder for you & for us to diagnose the problem, because
you don't show (and currently can't) the stacktrace to us.
> class xmlHandler(ContentHandler):
> def startElement(self, name, attrs):
> self.charList = []
If this shall gather some state over the coures of time, this is the
wrong place to initialize it, as it will get overwritten with each element.
> charName = []
> charID = []
There is no need to "prefill" variables. And it's confusing you do it
with a different type than what you assign to them below.
> if name == "row":
> charName = attrs.get("name", "")
> charID = attrs.get("characterID", "")
> print charName, '\t', charID
> self.buildlist(self,charName,charID)
> #self.test()
>
> def test(self):
> print "TeST"
> def buildlist(self,charName,charID):
> print charName, '\t',charID
>
> so here... test will print, but buildlist makes the parser stop and
> print "Error reading XML" after the first row it finds. Appending
> charName and charID to another variable makes it stop as well. I'm
> confused at this point
Please show us the stacktrace you suppress. Then help might be possible.
Diez
More information about the Python-list
mailing list