转载:@小五义http://www.cnblogs.com/xiaowuyi
原文地址:http://www.cnblogs.com/xiaowuyi/archive/2012/10/17/2727912.html
一、xml.dom的简单介绍
1、主要方法:
minidom.parse(filename):加载读取XML文件
doc.documentElement:获取XML文档对象
node.getAttribute(AttributeName):获取XML节点属性值
node.getElementsByTagName(TagName):获取XML节点对象集合
node.childNodes :返回子节点列表。
node.childNodes[index].nodeValue:获取XML节点值
node.firstChild:访问第一个节点,等价于pagexml.childNodes[0]
返回Node节点的xml表示的文本:
doc = minidom.parse(filename)
doc.toxml('UTF-8')
访问元素属性:
Node.attributes["id"]
a.name #就是上面的 "id"
a.value #属性的值
2、举例说明
例如:文件名book.xml
<?xml version="1.0" encoding="utf-8"?>
<info>
<intro>Book message</intro>
<list id='001'>
<head>bookone</head>
<name>python check</name>
<number>001</number>
<page>200</page>
</list>
<list id='002'>
<head>booktwo</head>
<name>python learn</name>
<number>002</number>
<page>300</page>
</list>
</info>
对上面的xml进行解析
代码如下:
#@小五义 http://www.cnblogs.com/xiaowuyi
#xml 解析
import xml.dom.minidom
dom1=xml.dom.minidom.parse('book.xml')
root=dom1.documentElement
book={}
booknode=root.getElementsByTagName('list')
for booklist in booknode:
print '='*20
print 'id:'+booklist.getAttribute('id')
for nodelist in booklist.childNodes:
if nodelist.nodeType ==1:
print nodelist.nodeName+':',
for node in nodelist.childNodes:
print node.data
运行结果为:
====================
id:001
head: bookone
name: python check
number: 001
page: 200
====================
id:002
head: booktwo
name: python learn
number: 002
page: 300
当然还有其他办法,就不继续转了。
本文介绍了如何使用Python中的xml.dom模块来解析XML文件。通过具体实例演示了如何读取XML文档、获取节点属性和值,以及遍历节点等基本操作。
856

被折叠的 条评论
为什么被折叠?



