解析xml

上一篇 / 下一篇  2017-10-28 22:13:55 / 个人分类:python

xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<!--this is a test about xml.-->
<collection shelf="New Arrivals">
    <movie title="Enemy Behind">
        <type>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>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>
        <year>1989</year>
        <rating>PG</rating>
        <stars>2</stars>
        <description>Viewable boredom</description>
    </movie>
</collection>
解析文件:
#encoding=utf-8
from xml.dom .minidom  import parse
dom=parse(r"f:\\movies.xml")
collection=dom.documentElement
print u"根节点是%s" %(collection)
print u"xml内容:%s" %collection.toxml()
print collection.hasAttribute("shelf")
print u"根节点的子节点:%s" %collection.childNodes
movies=collection.getElementsByTagName("movie")
print u"movie标签:%s" %movies
print u"第一个movie节点%s" %(movies[0])
print movies[0].childNodes
print len(movies[0].childNodes)
for i  in range(4):
    print  u"第%s个节点下的内容:" % i
    for j in range(1,13,2):
        print movies[i].childNodes[j]
        print "%s:%s" %(movies[i].childNodes[j].tagName,movies[i].childNodes[j].childNodes[0].data)

for k in range(4):
    type=movies[k].getElementsByTagName("type")[0]
    print type
    print type.childNodes[0].data
   
运行结果:
F:\>python z.txt
根节点是<DOM Element: collection at 0x33373c8>
xml内容:<collection shelf="New Arrivals">
    <movie title="Enemy Behind">
        <type>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>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>
        <year>1989</year>
        <rating>PG</rating>
        <stars>2</stars>
        <description>Viewable boredom</description>
    </movie>
</collection>
True
根节点的子节点:[<DOM Text node "u'\n    '">, <DOM Element: movie at 0x3337548>, <DOM Text node "u'\n    '">, <DOM Element: movie at 0x3337d48>, <DOM Text node "u'\n    '">, <DOM Element: movie at 0x3343588>, <DOM Text node "u'\n    '">, <DOM Element: movie at 0x3343d88>, <DOM Text node "u'\n'">]
movie标签:[<DOM Element: movie at 0x3337548>, <DOM Element: movie at 0x3337d48>, <DOM Element: movie at 0x3343588>, <DOM Element: movie at 0x3343d88>]
第一个movie节点<DOM Element: movie at 0x3337548>
[<DOM Text node "u'\n        '">, <DOM Element: type at 0x3337708>, <DOM Text node "u'\n        '">, <DOM Element: format at 0x3337808>, <DOM Text node "u'\n        '">, <DOM Element: year at 0x3337908>, <DOM Text node "u'\n        '">, <DOM Element: rating at 0x3337a08>, <DOM Text node "u'\n        '">, <DOM Element: stars at 0x3337b08>, <DOM Text node "u'\n        '">, <DOM Element: description at 0x3337c08>, <DOM Text node "u'\n    '">]
13
第0个节点下的内容:
<DOM Element: type at 0x3337708>
type:War, Thriller
<DOM Element: format at 0x3337808>
format:DVD
<DOM Element: year at 0x3337908>
year:2003
<DOM Element: rating at 0x3337a08>
rating:PG
<DOM Element: stars at 0x3337b08>
stars:10
<DOM Element: description at 0x3337c08>
description:Talk about a US-Japan war
第1个节点下的内容:
<DOM Element: type at 0x3337f08>
type:Anime, Science Fiction
<DOM Element: format at 0x3343048>
format:DVD
<DOM Element: year at 0x3343148>
year:1989
<DOM Element: rating at 0x3343248>
rating:R
<DOM Element: stars at 0x3343348>
stars:8
<DOM Element: description at 0x3343448>
description:A schientific fiction
第2个节点下的内容:
<DOM Element: type at 0x3343748>
type:Anime, Action
<DOM Element: format at 0x3343848>
format:DVD
<DOM Element: episodes at 0x3343948>
episodes:4
<DOM Element: rating at 0x3343a48>
rating:PG
<DOM Element: stars at 0x3343b48>
stars:10
<DOM Element: description at 0x3343c48>
description:Vash the Stampede!
第3个节点下的内容:
<DOM Element: type at 0x3343f48>
type:Comedy
<DOM Element: format at 0x334a088>
format:VHS
<DOM Element: year at 0x334a188>
year:1989
<DOM Element: rating at 0x334a288>
rating:PG
<DOM Element: stars at 0x334a388>
stars:2
<DOM Element: description at 0x334a488>
description:Viewable boredom
<DOM Element: type at 0x3337708>
War, Thriller
<DOM Element: type at 0x3337f08>
Anime, Science Fiction
<DOM Element: type at 0x3343748>
Anime, Action
<DOM Element: type at 0x3343f48>
Comedy



TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-03-27  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 14023
  • 日志数: 20
  • 建立时间: 2016-10-19
  • 更新时间: 2018-01-27

RSS订阅

Open Toolbar