当前位置: 代码迷 >> 综合 >> python使用xml.dom.minidom解析xml
  详细解决方案

python使用xml.dom.minidom解析xml

热度:33   发布时间:2024-02-22 18:22:12.0

 

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><rating>PG</rating><stars>2</stars><description>Viewable boredom</description>
</movie>
</collection>

 

1.示例

import xml.dom.minidomdef print_node(node):print("node type:"+str(node.nodeType))print("node name:"+node.nodeName)print("node value:"+str(node.nodeValue))print("attributes:"+str(node.attributes.items()))# 解析xml文件,获取dom树
tree = xml.dom.minidom.parse('./movies.xml')
# 根文档元素
collection = tree.documentElementprint_node(collection)movie_list = collection.getElementsByTagName("movie")
for movie in movie_list:print_node(movie)print("电影标题为:"+movie.getAttribute('title'))type_node_list = movie.getElementsByTagName("type")type = type_node_list[0].childNodes[0].data if len(type_node_list)>0 else ''print("type:"+str(type))format_node_list = movie.getElementsByTagName("format")format = format_node_list[0].childNodes[0].data if len(format_node_list)>0 else ''print("format:"+str(format))year_node_list = movie.getElementsByTagName("year")year = year_node_list[0].childNodes[0].data if len(year_node_list)>0 else ''print("year:"+str(year))rating_node_list = movie.getElementsByTagName("rating")rating = rating_node_list[0].childNodes[0].data if len(rating_node_list)>0 else ''print("rating:"+str(rating))stars_node_list = movie.getElementsByTagName("stars")stars = stars_node_list[0].childNodes[0].data if len(stars_node_list)>0 else ''print("stars:"+str(stars))description_node_list = movie.getElementsByTagName("description")description = description_node_list[0].childNodes[0].data if len(description_node_list)>0 else ''print("description:"+str(description))
node type:1
node name:collection
node value:None
attributes:<xml.dom.minidom.NamedNodeMap object at 0x000001FB2FE32740>
node type:1
node name:movie
node value:None
attributes:<xml.dom.minidom.NamedNodeMap object at 0x000001FB2FE2F7C0>
电影标题为:Enemy Behind
type:War, Thriller
format:DVD
year:2003
rating:PG
stars:10
description:Talk about a US-Japan war
node type:1
node name:movie
node value:None
attributes:<xml.dom.minidom.NamedNodeMap object at 0x000001FB2FE35340>
电影标题为:Transformers
type:Anime, Science Fiction
format:DVD
year:1989
rating:R
stars:8
description:A schientific fiction
node type:1
node name:movie
node value:None
attributes:<xml.dom.minidom.NamedNodeMap object at 0x000001FB2FE35440>
电影标题为:Trigun
type:Anime, Action
format:DVD
PS D:\workspace\github\python\python-demo\util\xml\dom> python .\simple.py
node type:1
node name:collection
node value:None
attributes:[('shelf', 'New Arrivals')]
node type:1
node name:movie
node value:None
attributes:[('title', 'Enemy Behind')]
电影标题为:Enemy Behind
type:War, Thriller
format:DVD
year:2003
rating:PG
stars:10
description:Talk about a US-Japan war 
node type:1
node name:movie
node value:None
attributes:[('title', 'Transformers')]
电影标题为:Transformers
type:Anime, Science Fiction
format:DVD
year:1989
rating:R
stars:8
description:A schientific fiction
node type:1
node name:movie
node value:None
attributes:[('title', 'Trigun')]
电影标题为:Trigun
type:Anime, Action
format:DVD
year:
rating:PG
stars:10
description:Vash the Stampede!
node type:1
node name:movie
node value:None
attributes:[('title', 'Ishtar')]
电影标题为:Ishtar
type:Comedy
format:VHS
year:
rating:PG
stars:2
description:Viewable boredom
  • xml.dom.minidom.parse:解析指定的xml文件

  • xml.dom.minidom.parseString:解析指定的xml字符串

  • tree.documentElement:获取文档根元素

  • node.nodeType:节点类型,符合xml规范,代表节点为元素、文本、注释、文档、属性、实体、引用

  • node.nodeName:节点名称,标签名

  • node.nodeValue:节点值,如果是文本节点则为文本内容,如果是属性节点,则为属性内容

  • node.attributes:节点属性,为NamedNodeMap类型对象,items()方法可以返回键值元组列表

  • node.getElementsByTagName():获取此节点下面的指定tag名称的节点列表

  • node.getAttribute():元素节点获取指定属性值

  • node.childNodes:子节点列表

 

 

 

 

 

 

 

 

 

 

 

 

  相关解决方案