解析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>
<!--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
from xml.dom .minidom import parse
dom=parse(r"f:\\movies.xml")
collection=dom.documentElement
print u"根节点是%s" %(collection)
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
print u"第一个movie节点%s" %(movies[0])
print movies[0].childNodes
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 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
根节点是<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:
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 14696
- 日志数: 20
- 建立时间: 2016-10-19
- 更新时间: 2018-01-27