Python and XML Help

ookrin ooklah at gmail.com
Tue Apr 14 22:29:01 EDT 2009


On Apr 12, 12:51 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> 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

Sorry for the delay, easter weekend:

I know you don't need to prefill the variables with python, and they
weren't there originally, I was just getting frustrated and trying
various things to see if they worked. There is actually no stacktrace
error. It will just print me the error message that I have when I try
to send the variables outside of the if loop. (from the try - except
statement.) I can still run click the button and it'll do it again.

There are three rows that I have to get out of my xml all the same
except with different values:
< row name="CharName" charID="IDNumber" >

So it prints out the first of the three lines and then just says
"Error Reading XML" if I'm running the buildlist() and trying to send
the variables out. As long as I don't try to do anything, it works
fine.

I can get these out and print them to the cmd line. But I want to
collect these in a variable and send it back off to my gui in pyqt4 to
list them. which is where i'm getting stuck.




More information about the Python-list mailing list