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)
最新推荐文章于 2024-10-11 08:30:15 发布