#!/usr/bin/python
# -*- coding: UTF-8 -*- import xml.sax class MovieHandler( xml.sax.ContentHandler ):
def __init__(self):
self.stack = []
# 元素开始事件处理
def startElement(self, tag, attributes):
if len(self.stack) !=0:
print "\n","*"*4*len(self.stack),
attr = ""
# 属性值
if attributes.getLength() > 0:
for attrName in attributes.getNames():
attr = " " + attr + attrName + "=" + attributes.getValue(attrName)
print "<"+tag+attr+">",
self.stack.append("start") # 元素结束事件处理
def endElement(self, tag):
if self.stack[len(self.stack)-1] == "content" and self.stack[len(self.stack)-2] == "start":
del self.stack[len(self.stack)-1]
del self.stack[len(self.stack)-2]
elif self.stack[len(self.stack)-1] =="start":
del self.stack[len(self.stack)-1]
print "\n","*"*4*len(self.stack), print "</"+tag+">", # 内容事件处理
def characters(self, content):
if content.strip() != '':
self.stack.append("content")
print content, if ( __name__ == "__main__"): # 创建一个 XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0) # 重写 ContextHandler
Handler = MovieHandler()
parser.setContentHandler( Handler ) parser.parse("movie.xml")

xml文档:

<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>

解析输出:

参见:http://www.cnblogs.com/hongfei/p/python-xml-sax.html

[python 学习] sax的更多相关文章

  1. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  2. Python学习--01入门

    Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...

  3. Python 学习小结

    python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...

  4. Python学习路径及练手项目合集

    Python学习路径及练手项目合集 https://zhuanlan.zhihu.com/p/23561159

  5. python学习笔记-python程序运行

    小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...

  6. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  7. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  8. [Python] 学习资料汇总

    Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大且完善的通用型语言,已经有十多年的发展历史,成熟且稳定.Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用 ...

  9. Python学习之路【目录】

    本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! 目录: Python学习[第一篇]python简介 Python学习[第二篇]p ...

随机推荐

  1. 【转】vux (scroller)上拉刷新、下拉加载更多

    1)比较关键的地方是要在 scroller 组件上里加一个 ref 属性 <scroller :lockX="true" height="-170" :p ...

  2. C++ Boost库的编译及使用

    https://www.jianshu.com/p/de1fda741beb https://www.cnblogs.com/weizhixiang/p/5804778.html Windows编译 ...

  3. The import org.apache.hadoop.mapreduce cannot be resolved

    ubuntu@VM---ubuntu:~$ sudo apt--src.tar.gz Reading package lists... Done Building dependency tree Re ...

  4. 004-spring-data-elasticsearch 3.0.0.0使用【二】-spring-data之定义方法、创建repository实例、从聚合根发布事件

    续上文 1.4.定义方法 存储库代理有两种方法可以从方法名称派生特定于存储的查询.它可以直接从方法名称派生查询,或者使用手动定义的查询.可用选项取决于实际store.但是,必须有一个策略来决定创建什么 ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_05 List集合_3_LinkedList集合

    链表实现,查询慢,增删快 addFirst 集合的开头添加 push等效于addFirst addLast getFirst和getLast clear 清空后 又输出 isEmpty 不为空才去获取 ...

  6. Microsoft SQL Server 2008 R2官方中文版(SQL2008下载)

    Microsoft SQL Server 2008 R2官方中文版(SQL2008下载) http://www.2cto.com/database/201308/235349.html

  7. DataGridView数值列和日期列

    本文转自:http://www.cnblogs.com/conexpress/p/5923324.html 在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会 ...

  8. Java类和对象的内存分配

    类的加载时机: 1.创建对象 2.调用类的静态成员 3.加载子类 类在实例化后的内存分配 1.每次创建对象时,都需要进行加载和创建2个操作: ① 先去判断需要的类是否已经加载,如果已经加载了,则无需再 ...

  9. Codeforces 1047C (线性筛+因数分解)

    题面 传送门 分析 1.暴力做法 首先先把每个数除以gcd(a1,a2-,an)gcd(a_1,a_2 \dots,a_n )gcd(a1​,a2​-,an​) 可以O(namax)O(n\sqrt ...

  10. Python学习第四十天函数的装饰器用法

    在软件开发的过程中,要遵循软件的一些原则封装的,不改变原有的代码的基础增加一些需求,python提供了装饰器来扩展函数功能,下面说说函数装饰器用法 def debug(func):      def ...