python 读取xml

时间:2023-03-09 13:43:53
python 读取xml
#!/usr/bin/python
# -*- coding: UTF- -*- 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')[]
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)