1.简单的XML文件内容如下:
<catalog>
<bookisbn="1-56592-724-9">
<title>The Cathedral& theBazaar</title>
<author>Eric S.Raymond</author>
</book>
<bookisbn="1-56592-051-1">
<title>Making TeXWork</title>
<author>NormanWalsh</author>
</book>
<!-- imagine more entries here...-->
</catalog>
2.代码
#! /usr/bin/python
import xml.dom.minidom
from xml.dom.minidom import Node
doc = xml.dom.minidom.parse("books.xml")
mapping = {}
for node in doc.getElementsByTagName_r("book"):
isbn =node.getAttribute("isbn")
L =node.getElementsByTagName_r("title")
for node2 in L:
title = ""
for node3 in node2.childNodes:
ifnode3.nodeType == Node.TEXT_NODE:
title += node3.data
mapping[isbn] = title
# mapping now has the samevalue as in the SAX example:
print(mapping)
不过这样写我感觉有问题!
以下是我整理的代码处理XML的常用操作:
1.如果配置信息如下:
<?xml version="1.0"encoding="UTF-8"?>
<config>
<routelist>routelist</routelist>
<route>route</route>
<ip>ip</ip>
<port>port</port>
<delay>delay</delay>
</config> 比如说:可以存储些连接配置DB的相关信息进来!
2.对应的解析XML的PY代码如下:
#=============================================
#作用:解析XML读取配置信息。整理归档,以后如果有
#DB信息要读取的话直接使用
#=============================================
from xml.dom.minidom import parse, parseString
def getText(nodelist):
rc =""
for node innodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
returnrc
if __name__=="__main__":
dom1 =parse('b.xml') # parse an XML file by name
config_element = dom1.getElementsByTagName_r("config")[0]
routelist =config_element.getElementsByTagName_r("routelist")[0]
route =config_element.getElementsByTagName_r("route")[0]
ip =config_element.getElementsByTagName_r("ip")[0]
port =config_element.getElementsByTagName_r("port")[0]
delay =config_element.getElementsByTagName_r("delay")[0]
printgetText(routelist.childNodes)
便可以将配置信息的内容全部遍历一遍的呀!
上面这种是XML中只有两级标签结构,如果有三层的话
<webapp>
<db>
<dbhost></dbhost>
</db>
<db>
<dbhost></dbhost>
</db>
</webapp>
案例二:
XML格式如下:
<webapp>
<db>
<dbhost>192.168.100.40</dbhost>
<dbusr>root</dbusr>
<dbpw>321</dbpw>
<dbname>mydb</dbname>
<dbtable>error</dbtable>
<dbtype>mysql</dbtype>
</db>
<db>
<dbhost>192.168.100.40</dbhost>
<dbusr>root</dbusr>
<dbpw>321</dbpw>