[python 学习] sax
#!/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的更多相关文章
- Python学习--04条件控制与循环结构
Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...
- Python学习--01入门
Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- Python学习路径及练手项目合集
Python学习路径及练手项目合集 https://zhuanlan.zhihu.com/p/23561159
- python学习笔记-python程序运行
小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
- [Python] 学习资料汇总
Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大且完善的通用型语言,已经有十多年的发展历史,成熟且稳定.Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用 ...
- Python学习之路【目录】
本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! 目录: Python学习[第一篇]python简介 Python学习[第二篇]p ...
随机推荐
- Spring Boot 异步方法的调用
Spring Boot 异步方法的调用 参考资料: 1.Spring Boot中使用@Async实现异步调用 使用方法 两个步骤: 1.开启配置 @EnableAsync,这一步特别容易忘记,导致测试 ...
- SpringBoot:配置文件及自动配置原理
西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...
- 【后台管理系统】—— Ant Design Pro组件使用(一)
一.搜索Search 搜索框 <Search placeholder="请输入关键字" defaultValue={kw && kw != 'nul ...
- The Linux usage model for device tree data
Linux and the Device Tree The Linux usage model for device tree data Author: Grant Likely grant.like ...
- Python分析《武林外传》 -----转载
转载原链接:http://www.cnblogs.com/fredkeke/p/8328387.html 我一向比较喜欢看武侠电影.小说,但是06年武林外传开播的时候并没有追剧,简单扫几眼之后发现他们 ...
- 2017埙箫简谱清单分享(附音频Demo)
前言 习箫五载,略有所获,皆在坚持. 本博文记录旨在记录练习过程中所录制的Demo以供自省.自娱.自乐,同时记录.分享简谱与箫友(目前为简谱,日后学会线谱后会添加相应谱子分类). 简谱 &&a ...
- ABI与API的区别
ABI是定义二进制级别的,两个模块的接口 比如一个二进制模块想要调用另外一个二进制模块提供的功能,它需要知道怎样通过汇编语言(即机器指令)去调用,以及怎样传递相应的参数和返回值(通过寄存器还是栈内存, ...
- Whatever happens tomorrow, we've had today
bathe: v. 用水清洗 resemblance:n. 相似.相像 cavity:n. 洞,孔,腔 stubborn: adj. 顽强的,固执的 stillness: n. 静止,沉静 tenth ...
- hacker101----XSS Review
所有你见过XSS行动在这一点上,但我们来回顾一下今天我们要讨论的XSS类型: 反射型XSS -- 来自用户的输入将直接返回到浏览器,从而允许注入任意内容 [浏览器输入,马上到服务器上,再反射回来直 ...
- C#中拼音模糊匹配汉字智能搜索
准备: 微软官方出了一个专用的汉字转拼音包Microsoft Visual Studio International Pack 1.0 SR1 首先到官网http://www.microsoft.co ...