Extracting Text from XML Documents : expat « XML « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » XML » expat 




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 %s\n=" % xmlFile
print xText.textBuff














20.3.expat
20.3.1.Extracting Text from XML Documents
20.3.2.Parsing XML Tags
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.