movies.xml

<collection shelf = "New Arrivals">
<movie title = "Enemy Behind">
<type>War, Thriller</type>
<format>DVD</format>
<year></year>
<rating>PG</rating>
<stars></stars>
<description>Talk about a US-Japan war</description>
</movie>
<movie title = "Transformers">
<type>Anime, Science Fiction</type>
<format>DVD</format>
<year></year>
<rating>R</rating>
<stars></stars>
<description>A schientific fiction</description>
</movie>
<movie title = "Trigun">
<type>Anime, Action</type>
<format>DVD</format>
<episodes></episodes>
<rating>PG</rating>
<stars></stars>
<description>Vash the Stampede!</description>
</movie>
<movie title = "Ishtar">
<type>Comedy</type>
<format>VHS</format>
<rating>PG</rating>
<stars></stars>
<description>Viewable boredom</description>
</movie>
</collection>

使用SAX API解析XML

#!/usr/bin/python3

import xml.sax

class MovieHandler( xml.sax.ContentHandler ):
def __init__(self):
self.CurrentData = ""
self.type = ""
self.format = ""
self.year = ""
self.rating = ""
self.stars = ""
self.description = "" # Call when an element starts
def startElement(self, tag, attributes):
self.CurrentData = tag
if tag == "movie":
print ("*****Movie*****")
title = attributes["title"]
print ("Title:", title) # Call when an elements ends
def endElement(self, tag):
if self.CurrentData == "type":
print ("Type:", self.type)
elif self.CurrentData == "format":
print ("Format:", self.format)
elif self.CurrentData == "year":
print ("Year:", self.year)
elif self.CurrentData == "rating":
print ("Rating:", self.rating)
elif self.CurrentData == "stars":
print ("Stars:", self.stars)
elif self.CurrentData == "description":
print ("Description:", self.description)
self.CurrentData = "" # Call when a character is read
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 if ( __name__ == "__main__"): # create an XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, ) # override the default ContextHandler
Handler = MovieHandler()
parser.setContentHandler( Handler ) parser.parse("movies.xml")

输出

*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Year:
Rating: PG
Stars:
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Year:
Rating: R
Stars:
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Stars:
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
Type: Comedy
Format: VHS
Rating: PG
Stars:
Description: Viewable boredom

使用DOM API解析XML

#!/usr/bin/python3

from xml.dom.minidom import parse
import xml.dom.minidom # Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
print ("Root element : %s" % collection.getAttribute("shelf")) # Get all the movies in the collection
movies = collection.getElementsByTagName("movie") # Print detail of each movie.
for movie in movies:
print ("*****Movie*****")
if movie.hasAttribute("title"):
print ("Title: %s" % movie.getAttribute("title")) type = movie.getElementsByTagName('type')[]
print ("Type: %s" % type.childNodes[].data)
format = movie.getElementsByTagName('format')[]
print ("Format: %s" % format.childNodes[].data)
rating = movie.getElementsByTagName('rating')[]
print ("Rating: %s" % rating.childNodes[].data)
description = movie.getElementsByTagName('description')[]
print ("Description: %s" % description.childNodes[].data)

输出

Root element : New Arrivals
*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Rating: PG
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Rating: R
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
Type: Comedy
Format: VHS
Rating: PG
Description: Viewable boredom

最新文章

  1. Atitit osi tcp ip 对应attilax总结
  2. haohantech浩瀚盘点机“PDA无线订货开单”终端 移动现场下单APP(打印扫描一体)
  3. SAP中数字计算时溢出捕获
  4. Http报头Accept与Content-Type的区别
  5. JS实现联想输入(一)
  6. zookeeper学习记录
  7. JQUERY 选择器 总结,比较全
  8. SVN 之 去掉SVN管理标记
  9. 设置BootStrap导航条的高度
  10. 【模拟】NCPC 2014 D Dice Game
  11. jQuery自定义组件——输入框设置默认提示文字
  12. Cortex-M3和Cortex-M4 Fault异常应用之二 ----- Fault处理函数的实现
  13. css3简单几步画一个乾坤图
  14. vue.js路由参数简单实例讲解------简单易懂
  15. JAVA_SE基础——59.权限访问修饰符
  16. Linux系统启动详解
  17. spring Ioc容器之使用XML配置Bean
  18. 在VS中连接MySQL
  19. 利用redis统计信息对CPU使用率进行收集
  20. FZU2259 : 图

热门文章

  1. 【BZOJ3529】[Sdoi2014]数表 莫比乌斯反演+树状数组
  2. oneThink的ArticleController控制,详看
  3. SVN创建主干,分支、合并分支
  4. 利用maven-assembly-plugin加载不同环境所需的配置文件及使用场景
  5. 项目无法运行iPhone5模拟器
  6. 小米范工具系列之六:小米范 web查找器2.x版本发布
  7. proc_create函数内幕初探
  8. 《Java程序猿面试笔试宝典》之Java程序初始化的顺序是如何的
  9. &#39;ascii&#39; codec can&#39;t decode byte 0xc4 in position 27: ordinal not in range(128)
  10. centos vim编辑器 第八节课