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实现插入排序和选择排序的方法)
- wxpython 弹出对话框显示图片(WxPython建立批量录入框窗口)
- python 串口图形化(python使用MQTT给硬件传输图片的实现方法)
- python实现购物网站(Python实战购物车项目的实现参考)
- pythonmatplotlib怎么用(Python matplotlib画图与中文设置操作实例分析)
- python函数大全详细(详解Python函数式编程—高阶函数)
- python 数据库实现学生管理系统(python+mysql实现教务管理系统)
- python删除list的指定元素(Python list列表中删除多个重复元素操作示例)
- python如何把字符串转换成数字(python实现字符串加密成纯数字)
- python获取天气源(Python3爬虫之自动查询天气并实现语音播报)
- python如何解压加密zip文件(python读取有密码的zip压缩文件实例)
- linux下python安装pip(在Docker中的ubuntu中安装Python3和Pip的问题)
- pythonnumpy定义一个2*2数组(对python numpy.array插入一行或一列的方法详解)
- pythonselenium自动化使用教程(selenium python 实现基本自动化测试的示例代码)
- pythonpulp怎么使用(pyhanlp安装介绍和简单应用)
- python中怎么查看函数的用法(Python中函数的基本定义与调用及内置函数详解)
- 一窗通办政务服务小品剧本(一窗通办政务服务小品剧本)
- 刘韬涛丁子贺小品《根治低头族》台词剧本(刘韬涛丁子贺小品根治低头族台词剧本)
- 看完《夺冠》,黄渤的演技我实在夸不起来,彭昱畅反令人惊喜(黄渤的演技我实在夸不起来)
- 黄渤泪目 我的痴呆父亲,我内心永远的痛(黄渤泪目我的痴呆父亲)
- 蒜苔和鱿鱼尾巴一起炒,味道特别棒,又脆又嫩,有滋又有味(蒜苔和鱿鱼尾巴一起炒)
- 鱿鱼炒蒜苔不是黑暗料理,这样做清香扑鼻,鲜美脆嫩,开胃又下饭(鱿鱼炒蒜苔不是黑暗料理)
热门推荐
- mysql与oracle如何直接连接(Oracle更换为MySQL遇到的问题及解决)
- html5导航栏怎么设计(html5 横向滑动导航栏的方法示例)
- vsftpd服务器的安装与配置(编译安装 vsFTP 3.0.3的详细解析)
- laravel权限控制(laravel-admin 在列表页添加自定义按钮的例子)
- SQL中FOR XML PATH
- ASP.NET中() => 的含义
- laravel多用户认证实例(Laravel框架验证码类用法实例分析)
- MySQL 使用事件(Events)完成计划任务(MySQL 使用事件Events完成计划任务)
- docker离线安装步骤(Docker安装FastDFS的方法步骤)
- 开发者微信公众号开发流程(微信公众平台开发教程⑥ 微信开发集成类的使用图文详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9