xml解析,练习

<collection shelf="New Arrivals">
<movie title="Enemy Behind">
   <type hehe="laji">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 hehe="food">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>

# -*- coding: utf-8 -*- from xml.dom.minidom import parse

#使用minidom解析器打开 XML 文档 docObj = parse("moves.xml") root = docObj.documentElement if root.hasAttribute("shelf"):     rootAttribute = root.getAttribute("shelf")

print(rootAttribute)

# 在集合中获取所有电影 movies = root.getElementsByTagName("movie")

for movie in movies:     print("-"*20)     if movie.hasAttribute("title"):         print(movie.getAttribute("title"))     typeMovie = movie.getElementsByTagName("type")[0]

if typeMovie.hasAttribute("hehe"):         print(typeMovie.getAttribute("hehe"))     print(typeMovie.childNodes[0].data)

上一篇:jQuery实现多级手风琴树形下拉菜单(源码)


下一篇:rsync+sersync实现数据文件实时同步