import xml.dom.minidom
class DomParser():
def parse(self):
documentTree=xml.dom.minidom.parse("movies.xml")#文档对象
collection=documentTree.documentElement#文档对象集
title=collection.getAttribute("shelf")
print("title:",title)
movies=collection.getElementsByTagName("movie")
for movie in movies:
type=movie.getElementsByTagName("type")[0]
print("type:",type.childNodes[0].data)
format=movie.getElementsByTagName("format")[0]
print("format:",format.childNodes[0].data)
year=movie.getElementsByTagName("year")[0]
print("year:",year.childNodes[0].data)
rating=movie.getElementsByTagName("rating")[0]
print("rating:",rating.childNodes[0].data)
stars=movie.getElementsByTagName("stars")[0]
print("stars:",stars.childNodes[0].data)
description=movie.getElementsByTagName("description")[0]
print("description:",description.childNodes[0].data)
if "__main__":
parser=DomParser()
parser.parse()
Dom解析(python)
最新推荐文章于 2023-02-02 16:58:11 发布
本文介绍了一个使用Python的xml.dom.minidom模块解析XML文件的方法。通过解析一个包含电影信息的XML文件,文章详细展示了如何获取电影的类型、格式、年份、评分、明星和描述等数据。
172

被折叠的 条评论
为什么被折叠?



