Python and XML Help

ookrin ooklah at gmail.com
Sun Apr 12 00:10:20 EDT 2009


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"

class xmlHandler(ContentHandler):
    def startElement(self, name, attrs):
        self.charList = []
        charName = []
        charID = []
        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



More information about the Python-list mailing list