Extracting Text from XML Documents : Expat : XML PYTHON TUTORIALS


PYTHON TUTORIALS » XML » Expat »

 

Extracting Text from XML Documents


from xml.parsers import expat

xmlFile = "emails.xml"

#Define a class that will store the character data
class xmlText(object):
    def __init__ (self):
        self.textBuff = ""
    def CharacterData(self, data):
        data = data.strip()
        if data:
            data = data.encode('ascii')
            self.textBuff += data + "n"

    def Parse(self, fName):
        xmlParser = expat.ParserCreate()
        xmlParser.CharacterDataHandler = self.CharacterData
        xmlParser.Parse(open(fName).read()1)

xText = xmlText()
xText.Parse(xmlFile)
print "Text from %sn=" % xmlFile
print xText.textBuff



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo XML
» Expat