<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>
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 = "" # 元素开始调用
def startElement(self, tag, attributes):
self.CurrentData = tag
if tag == "movie":
print ("*****Movie*****")
title = attributes["title"]
print ("Title:", title) # 元素结束调用
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 = "" # 读取字符时调用
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__"): # 创建一个 XMLReader
parser = xml.sax.make_parser()
# 关闭命名空间
parser.setFeature(xml.sax.handler.feature_namespaces, 0) # 重写 ContextHandler
Handler = MovieHandler()
parser.setContentHandler( Handler ) parser.parse("movies.xml")
from xml.dom.minidom import parse
import xml.dom.minidom # 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
print ("Root element : %s" % collection.getAttribute("shelf")) # 在集合中获取所有电影
movies = collection.getElementsByTagName("movie") # 打印每部电影的详细信息
for movie in movies:
print ("*****Movie*****")
if movie.hasAttribute("title"):
print ("Title: %s" % movie.getAttribute("title")) type = movie.getElementsByTagName('type')[0]
print ("Type: %s" % type.childNodes[0].data)
format = movie.getElementsByTagName('format')[0]
print ("Format: %s" % format.childNodes[0].data)
rating = movie.getElementsByTagName('rating')[0]
print ("Rating: %s" % rating.childNodes[0].data)
description = movie.getElementsByTagName('description')[0]
print ("Description: %s" % description.childNodes[0].data)

吴裕雄--天生自然 PYTHON3开发学习:XML 解析的更多相关文章

  1. 吴裕雄--天生自然 PYTHON3开发学习:MySQL - mysql-connector 驱动

    import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user=&q ...

  2. 吴裕雄--天生自然 PYTHON3开发学习:字符串

    var1 = 'Hello World!' var2 = "Runoob" #!/usr/bin/python3 var1 = 'Hello World!' var2 = &quo ...

  3. 吴裕雄--天生自然 PYTHON3开发学习:数字(Number)

    print ("abs(-40) : ", abs(-40)) print ("abs(100.10) : ", abs(100.10)) #!/usr/bin ...

  4. 吴裕雄--天生自然 PYTHON3开发学习:运算符

    #!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("1 - c 的值为:", c) c = a - b print ( ...

  5. 吴裕雄--天生自然 PYTHON3开发学习:基本数据类型

    #!/usr/bin/python3 counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = "runoob" # 字符串 print ...

  6. 吴裕雄--天生自然 PYTHON3开发学习:基础语法

    #!/usr/bin/python3 # 第一个注释 print ("Hello, Python!") # 第二个注释 #!/usr/bin/python3 # 第一个注释 # 第 ...

  7. 吴裕雄--天生自然 PYTHON3开发学习:函数

    def 函数名(参数列表): 函数体 # 计算面积函数 def area(width, height): return width * height def print_welcome(name): ...

  8. 吴裕雄--天生自然 PYTHON3开发学习:元组

    tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", ...

  9. 吴裕雄--天生自然 PYTHON3开发学习:列表

    list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b& ...

随机推荐

  1. FMDB 加入数据库版本信息

    //创建数据库版本表 if (![db tableExists:@"tb_dbInfo"]) { result = [db executeUpdate:@"CREATE ...

  2. C++ STD Gems05

    find.find_if.find_first_of.mismatch.search.adjacent_find #include <iostream> #include <vect ...

  3. select、poll和epoll机制

    一.参考网址 1.select函数及fd_set介绍 2.linux select 函数和 fd_set 用法 2.select.poll和epoll的区别 3.利用select实现IO多路复用TCP ...

  4. @SpringBootApplication-exclude和扫描并装配其他包下的bean(@AliasFor)

    1.exclude 不装配指定bean @SpringBootApplication(exclude={com.ebc.User.class}) 2.scanBasePackages package ...

  5. DP背包问题学习笔记及系列练习题

    01 背包: 01背包:在M件物品中取出若干件物品放到背包中,每件物品对应的体积v1,v2,v3,....对应的价值为w1,w2,w3,,,,,每件物品最多拿一件. 和很多DP题一样,对于每一个物品, ...

  6. POJ - 3977 Subset(二分+折半枚举)

    题意:有一个N(N <= 35)个数的集合,每个数的绝对值小于等于1015,找一个非空子集,使该子集中所有元素的和的绝对值最小,若有多个,则输出个数最小的那个. 分析: 1.将集合中的元素分成两 ...

  7. Sequence Models Week 3 Neural Machine Translation

    Neural Machine Translation Welcome to your first programming assignment for this week! You will buil ...

  8. 冒泡排序_python

    def popdata(ls): for i in range(len(ls)): for j in range(i+1,len(ls)): if ls[i]>ls[j]: # tmp=ls[i ...

  9. 第32&35章 数据库的安装&存储实力的管理

    第32章 数据库的安装IO取决于磁盘的个数和接口带宽 版本安装顺序是从低到高存储架构师 第35章 存储实例的管理ASM配置说白了就是ORACLE自己的,不通过操作系统对磁盘进行管理.fdisk -l查 ...

  10. pywin32获得tkinter窗口句柄,并在上面绘图

    想实现用win32 API在tkinter窗口上画图,那么应该先获得tkinter窗口的句柄hwnd,然后再获得tkinter的设备hdc.尝试了FindWindow(),GetActiveWindo ...