python处理各种xml文件(Python使用sax模块解析XML文件示例)
类别:脚本大全 浏览量:1953
时间:2021-10-22 07:30:37 python处理各种xml文件
Python使用sax模块解析XML文件示例本文实例讲述了Python使用sax模块解析XML文件。分享给大家供大家参考,具体如下:
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 > |
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" ) |
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线格式化XML/在线压缩XML:https://tool.zzvips.com/t/xml/
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://www.cnblogs.com/wcwnina/p/7233386.html
您可能感兴趣
- python处理时间序列常用方法汇总(python整小时 整天时间戳获取算法示例)
- 基于pythonopencv的图片识别(Python Opencv实现图像轮廓识别功能)
- python 装饰器模式(python重试装饰器的简单实现方法)
- python数据分析pandas教程(11个Python Pandas小技巧让你的工作更高效附代码实例)
- python处理水印(Yii 使用intervention/image拓展实现图像处理功能)
- centos7上安装python(centos6.5安装python3.7.1之后无法使用pip的解决方案)
- mongodb python教程(python使用pymongo操作mongo的完整步骤)
- pyqt一键部署python环境(python使用PyQt5的简单方法)
- python计算csv的行数(对Python 多线程统计所有csv文件的行数方法详解)
- python详细讲解类方法的使用(浅谈python标准库--functools.partial)
- python中jieba库怎么用(详解Python数据可视化编程 - 词云生成并保存jieba+WordCloud)
- python端口扫描脚本测试(Python实现的IP端口扫描工具类示例)
- python爬虫入门自学(自学python爬虫的建议和周期预算)
- python 自定义获取文件目录(Python使用os.listdir和os.walk获取文件路径与文件下所有目录的方法)
- python对mysql数据分析(python使用adbapi实现MySQL数据库的异步存储)
- 怎么查看python的安装路径(查看python安装路径及pip安装的包列表及路径)
- 阴阳师孟婆御魂推荐 孟婆御魂搭配毕业套(阴阳师孟婆御魂推荐)
- 袁冰妍终于接到新剧,饰演反追男主,看到合作演员 眼光果然毒辣(袁冰妍终于接到新剧)
- 记忆中的台词(记忆中的台词)
- 袁冰妍轧戏 拍《琉璃》的同时还在拍《将夜》,难怪被骂演技差(拍琉璃的同时还在拍将夜)
- 刚红就耍大牌,《琉璃》角色滤镜碎一地,心疼工作人员(琉璃角色滤镜碎一地)
- 袁冰妍郑业成这对可以处,有脸红情话他们是真的敢说(袁冰妍郑业成这对可以处)
热门推荐
- table 细边框
- python如何使用multiprocess(Python multiprocessing多进程原理与应用示例)
- docker怎么部署node-exporter(Docker搭建部署Node项目的方法步骤)
- mybatis批量插入报错(解决myBatis中删除条件的拼接问题)
- smart工具包括哪些元素(Smarty模板语法详解)
- php微信公众号管理后台(php实现微信公众号创建自定义菜单功能的实例代码)
- vue加载html5动画(vue实现旋转木马动画)
- 抖音上很火的表白程序链接(我喜欢你 抖音表白程序python版)
- 微信小程序贪吃蛇大作战(微信小程序实现贪吃蛇游戏)
- docker本地项目生成镜像(docker images本地迁移的实现)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9