#!/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. POJ 1384 Piggy-Bank (完全背包)

    Piggy-Bank 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/F Description Before ACM can d ...

  2. Redis的消息订阅/发布 Utils工具类

    package cn.cicoding.utils; import org.json.JSONException; import org.json.JSONObject; import redis.c ...

  3. SQLserver基础--连接查询、联合查询、索引

    一.子查询补充: Exists的用法:select*from haha where exists(select*from bumen where bumen.code=haha.bumen,and b ...

  4. 网络流小记(EK&dinic&当前弧优化&费用流)

    欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...

  5. CyclicBarrier 源码分析

    CyclicBarrier CyclicBarrier 是一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point) 之后同时释放执行.CyclicB ...

  6. VMware 虚拟化编程(1) — VMDK/VDDK/VixDiskLib/VADP 概念简析

    目录 目录 VMDK VDDK VixDiskLib VADP VMDK VMDK(VMware's Virtual Machine Disk Format,VMware 虚拟磁盘格式):简单来说就是 ...

  7. 测开之路一百二十三:快速搭建python虚拟环境

    前提:已装好python3.4+且环境可正常运行 一:手动搭建: 准备好一个工作目录 管理员运行cmd,进入到准备的目录里面 执行命令:python -m venv 虚拟环境名 激活虚拟环境(在ven ...

  8. clientdataset 做为 单机数据库的 使用 学习

    http://blog.csdn.net/waveyang/article/details/34146737 unit Unit3; interface uses Winapi.Windows, Wi ...

  9. 分享一篇Linux系统使用Tomcat服务时交互式修改server.xml中端口号的shell脚本

    #!/bin/bash echo -e '\n' echo "***********************************" port1=`grep -r "s ...

  10. 如何在Web工程中实现任务计划调度

    转载自: http://www.oschina.net/question/146385_37793?sort=time 下面就Servlet侦听器结合Java定时器来讲述整个实现过程.要运用Servl ...