#!/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. lists.newarraylist()和new arraylist() 区别

    转自 https://blog.csdn.net/qq_2300688967/article/details/79490345 lists.newarraylist(): List<String ...

  2. MySQL高可用方案 MHA之一MHA安装

    MHA0.58安装 MHA(Master High Availability)由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点).管理节点mha4mysql-manager ...

  3. python监控ip攻击,服务器防火墙

    '''写一个程序,监控nginx的日志,如果有人攻击就加入黑名单 把ip加入黑名单的策略是,1分钟之内,如果同一个ip请求超过200次,那就加入黑名单''' '''分析:1.打开文件 2.循环读取 3 ...

  4. curl 使用 post 请求,传递 json 参数,下载文件

    curl -X POST http://ip:8888/nacos/v1/cs/file/download -H "Accept: application/octet-stream" ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第1节 基础加强_4_Junit_@Before&@After

    为了演示输出一段话 测试add的方法 虽然报错了 但是打印的结果还是输出

  6. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_3_BufferedInputStream_字节缓冲

    内容改成abc 来个数组缓冲

  7. git.ZC_命令积累

    1.删除文件 git rm 想要删除的文件的名字及其后缀 git commit -m "对本次提交的描述信息" git push 删除文件夹,执行命令: git rm 想要删除的文 ...

  8. Mac入门--如何使用brew安装多个PHP版本

    一 安装7.1 1. 安装PHP7.1 brew install php@7.1 2. 修改配置 php-fpm.conf,一般在/usr/local/etc/php下(如果php-fpm.conf中 ...

  9. python函数纯概念汇总(一)

    在使用python的时候由于前期基本概念没有分清楚,所以需要重新归纳汇总学一学. 一.什么是函数: 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很 ...

  10. [Web 前端] 015 css 三种元素的介绍

    1. 块元素,内联元素,内联块元素 元素就是标签 布局中常用的有三种标签 块元素 内联元素 内联块元素 1.1 块元素 也称为行元素 布局中常用的标签,如 div.p.ul.li.h1~h6.dl.d ...