Python sax模块(SAX解析XML)
2017-07-25 12:22
459 查看
XML样例:
<?xml version="1.0"?> <collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>View Code
SAX解析代码展示:
from xml import sax class MovieHandler(sax.ContentHandler): def __init__(self): # 初始化数据,并增加一个当前数据 self.CurrentData = "" self.type = "" self.format = "" self.year = "" self.rating = "" self.stars = "" self.description = "" # 文档启动的时候调用 def startDocument(self): print('XML开始解析中...') # 元素开始事件处理 def startElement(self, name, attrs): self.CurrentData=name if self.CurrentData=='movie': print('*********movie*********') title=attrs['title'] print('Title:{0}'.format(title)) # 内容事件处理 def characters(self, content): if self.CurrentData == "type": self.type = content elif self.CurrentData == "format": self.format = content elif self.CurrentData == "year": self.year = content elif self.CurrentData == "rating": self.rating = content elif self.CurrentData == "stars": self.stars = content elif self.CurrentData == "description": self.description = content # 元素结束事件处理 def endElement(self, name): if self.CurrentData=='type': print('Type:{0}'.format(self.type)) elif self.CurrentData=='format': print('Format:{0}'.format(self.format)) elif self.CurrentData=='year': print('Year:{0}'.format(self.year)) elif self.CurrentData == 'rating': print('Rating:{0}'.format(self.rating)) elif self.CurrentData == 'stars': print('Stars:{0}'.format(self.stars)) elif self.CurrentData == 'description': print('Description:{0}'.format(self.description)) self.CurrentData = "" # 文档结束的时候调用 def endDocument(self): print('XML文档解析结束!') if __name__=='__main__': handler=MovieHandler() parser = sax.make_parser() # parser.setFeature(sax.handler.feature_namespaces, 0) parser.setContentHandler(handler) parser.parse("sax_test.xml")
至此,转载请注明出处。
相关文章推荐
- Python解析XML,sax模块的介绍
- Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用
- 使用Python+XML添加自定义模块
- python解析xml模块封装代码
- python sax方式解析xml
- Python通过DOM和SAX方式解析XML的应用实例分享
- Python3之XML模块
- python 之模块之 xml.dom.minidom解析xml
- python模块xml,xml.etree.ElementTree 节点的读取,查询,修改,保存,创建,删除等
- Python minidom模块(DOM写入和解析XML)
- sax模块处理xml
- python的XML处理模块ElementTree
- python开发_xml.etree.ElementTree_XML文件操作_该模块在操作XML数据是存在安全隐患_慎用
- Python中使用SAX解析XML及实例
- python开发_xml.etree.ElementTree_XML文件操作_该模块在操作XML数据是存在安全隐患_慎用
- 【慕课笔记】3-3 应用SAX方式解析XML—使用SAX解析XML文件的节点名和节点间文本
- python模块:xml
- xml数据处理--sax模块使用
- 【慕课笔记】3-2 应用SAX方式解析XML—使用SAX解析XML文件的节点属性
- python数据交换模块-XML