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

yipeiwu_com5年前Python基础

本文实例讲述了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/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python操作xml数据技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python打开使用的方法

python打开使用的方法

python怎么打开使用? 1、首先需要打开电脑的桌面,如图所示,并按开始的快捷键点击安装好的python3.6程序进入。 2、然后点击进入之后,如图所示,可以看到页面上面的三个大于号...

python中lambda函数 list comprehension 和 zip函数使用指南

lambda 函数 Python 支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做 lambda 的函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。 def f...

Python绘制频率分布直方图的示例

Python绘制频率分布直方图的示例

项目中在前期经常要看下数据的分布情况,这对于探究数据规律非常有用。概率分布表示样本数据的模样,长的好不好看如果有图像展示出来就非常完美了,使用Python绘制频率分布直方图非常简洁,因为...

Python高级用法总结

列表推导(list comprehensions) 场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表。 最简单的方法:新建列表,遍历原三维列表,判断一维数据是否为a,...

Python动态导入模块的方法实例分析

本文实例讲述了Python动态导入模块的方法。分享给大家供大家参考,具体如下: 一、正常导入模块 正常模块导入方式: import module(模块路径) 同时导入多个模块: im...