xml解析
发布时间:2020-12-16 00:03:19 所属栏目:百科 来源:网络整理
导读:collectionshelf="NewArrivals"movietitle="EnemyBehind"typeWar,Thriller/typeformatDVD/formatyear2003/yearratingPG/ratingstars10/starsdescriptionTalkaboutaUS-Japanwar/description/moviemovietitle="Transformers"typeAnime,ScienceFiction/typeform
<collectionshelf="NewArrivals"> <movietitle="EnemyBehind"> <type>War,Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>TalkaboutaUS-Japanwar</description> </movie> <movietitle="Transformers"> <type>Anime,ScienceFiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>Aschientificfiction</description> </movie> <movietitle="Trigun"> <type>Anime,Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>VashtheStampede!</description> </movie> <movietitle="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewableboredom</description> </movie> </collection> ####################################################################################### __author__='sea' #coding:utf-8 fromxml.dom.minidomimportparse importxml.dom.minidom #加载读取xml文件 DOMTree=xml.dom.minidom.parse("XTest.xml") #读取xml对象 collection=DOMTree.documentElement ''' ifcollection.hasAttribute("shelf"): print("Rootelement:%s"%collection.getAttribute("shelf")) ''' #获取xml节点集合 movies=collection.getElementsByTagName("movie") formovieinmovies: #获取xml节点属性值 ifmovie.hasAttribute("title"): print("Title:%s"%movie.getAttribute("title")) type=movie.getElementsByTagName('type')[0] #返回子节点列表 print(type.childNodes[0].nodeValue) #print("Type:%s"%type.childNodes[0].data) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |