Python使用sax模块解析XML文件示例


Posted in Python onApril 04, 2019

本文实例讲述了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")
Python 相关文章推荐
python利用hook技术破解https的实例代码
Mar 25 Python
深入学习Python中的装饰器使用
Jun 20 Python
Python面向对象编程中关于类和方法的学习笔记
Jun 30 Python
Python使用wxPython实现计算器
Jan 30 Python
利用python库在局域网内传输文件的方法
Jun 04 Python
python逆序打印各位数字的方法
Jun 25 Python
Python闭包思想与用法浅析
Dec 27 Python
Python内置random模块生成随机数的方法
May 31 Python
Python中函数参数匹配模型详解
Jun 09 Python
python使用PIL和matplotlib获取图片像素点并合并解析
Sep 10 Python
VS2019+python3.7+opencv4.1+tensorflow1.13配置详解
Apr 16 Python
2020版Python学习路线图(附学习资料)
Sep 15 Python
详解小白之KMP算法及python实现
Apr 04 #Python
Python魔法方法功能与用法简介
Apr 04 #Python
详解pandas.DataFrame中删除包涵特定字符串所在的行
Apr 04 #Python
pandas删除指定行详解
Apr 04 #Python
详解python之heapq模块及排序操作
Apr 04 #Python
python实现kmp算法的实例代码
Apr 03 #Python
详解python多线程之间的同步(一)
Apr 03 #Python
You might like
ThinkPHP模板替换与系统常量及应用实例教程
2014/08/22 PHP
PHP计算加权平均数的方法
2015/07/16 PHP
学习thinkphp5.0验证类使用方法
2017/11/16 PHP
javascript实现的在当前窗口中漂浮框的代码
2010/03/15 Javascript
弹出层之1:JQuery.Boxy (一) 使用介绍
2011/10/06 Javascript
js左侧三级菜单导航实例代码
2013/09/13 Javascript
jquery按回车提交数据的代码示例
2013/11/05 Javascript
jquery实现可拖动DIV自定义保存到数据的实例
2013/11/20 Javascript
JS正则表达式验证数字代码
2014/01/28 Javascript
js简单倒计时实现代码
2016/04/30 Javascript
Jquery uploadify 多余的Get请求(404错误)的解决方法
2017/01/26 Javascript
jQuery仿IOS弹出框插件
2017/02/18 Javascript
常用的js方法合集
2017/03/10 Javascript
JS之if语句对接事件动作逻辑(详解)
2017/06/28 Javascript
JS实现访问DOM对象指定节点的方法示例
2018/04/04 Javascript
JavaScript实现构造json数组的方法分析
2018/08/17 Javascript
微信小程序实现动态列表项的顺序加载动画
2019/07/25 Javascript
vue.js实现图书管理功能
2019/09/24 Javascript
在vue中高德地图引入和轨迹的绘制的实现
2019/10/11 Javascript
JS函数本身的作用域实例分析
2020/03/16 Javascript
小程序选项卡以及swiper套用(跨页面)
2020/06/19 Javascript
python使用dlib进行人脸检测和关键点的示例
2020/12/05 Python
一款纯css3实现的漂亮的404页面的实例教程
2014/11/27 HTML / CSS
精灵市场:Pixie Market
2019/06/18 全球购物
物控部经理职务说明书
2014/02/25 职场文书
计算机相关专业自荐信
2014/07/02 职场文书
查摆问题自我剖析材料
2014/08/18 职场文书
庆六一活动总结
2014/08/29 职场文书
民主评议政风行风整改方案
2014/09/17 职场文书
家长对孩子的寄语
2015/02/26 职场文书
劳动保障个人工作总结
2015/03/04 职场文书
放假通知
2015/04/14 职场文书
工作简报格式范文
2015/07/21 职场文书
关于战胜挫折的名言警句大全!
2019/07/05 职场文书
Redis 常见使用场景
2021/08/30 Redis
浅谈为什么我的 z-index 又不生效了
2022/07/15 HTML / CSS