xml version="1.0" encoding="utf-8"?>
<catalog>
<maxid>4maxid>
<login username="pytest" passwd=\'123456\'>
<caption>Pythoncaption>
<item id="4">
<caption>testcaption>
item>
login>
<item id="2">
<caption>Zopecaption>
item>
catalog>
#coding: utf-8
import xml.dom.minidom
dom = xml.dom.minidom.parse("del.xml") #打开xml文档
root = dom.documentElement #得到xml文档对象
print "nodeName:", root.nodeName #每一个结点都有它的nodeName,nodeValue,nodeType属性
print "nodeValue:", root.nodeValue #nodeValue是结点的值,只对文本结点有效
print "nodeType:", root.nodeType
print "ELEMENT_NODE:", root.ELEMENT_NODE
#coding: utf-8
import xml.dom.minidom
dom = xml.dom.minidom.parse("del.xml")
root = dom.documentElement
bb = root.getElementsByTagName(\'maxid\')
print type(bb)
print bb
b = bb[0]
print b.nodeName
print b.nodeValue
#coding: utf-8
import xml.dom.minidom
dom = xml.dom.minidom.parse("del.xml")
root = dom.documentElement
itemlist = root.getElementsByTagName(\'login\')
item = itemlist[0]
print item.getAttribute("username")
print item.getAttribute("passwd")
itemlist = root.getElementsByTagName("item")
item = itemlist[0] #通过在itemlist中的位置区分
print item.getAttribute("id")
item2 = itemlist[1] #通过在itemlist中的位置区分
print item2.getAttribute("id")
#coding: utf-8
import xml.dom.minidom
dom = xml.dom.minidom.parse("del.xml")
root = dom.documentElement
itemlist = root.getElementsByTagName(\'caption\')
item = itemlist[0]
print item.firstChild.data
item2 = itemlist[1]
print item2.firstChild.data
xml version="1.0" encoding="UTF-8" ?>
<users>
<user id="1000001">
<username>Adminusername>
<email>admin@live.cnemail>
<age>23age>
<sex>boysex>
user>
<user id="1000002">
<username>Admin2username>
<email>admin2@live.cnemail>
<age>22age>
<sex>boysex>
user>
<user id="1000003">
<username>Admin3username>
<email>admin3@live.cnemail>
<age>27age>
<sex>boysex>
user>
<user id="1000004">
<username>Admin4username>
<email>admin4@live.cnemail>
<age>25age>
<sex>girlsex>
user>
<user id="1000005">
<username>Admin5username>
<email>admin5@live.cnemail>
<age>20age>
<sex>boysex>
user>
<user id="1000006">
<username>Admin6username>
<email>admin6@live.cnemail>
<age>23age>
<sex>girlsex>
user>
# -*- coding:utf-8 -*-
from xml.dom import minidom
def get_attrvalue(node, attrname):
return node.getAttribute(attrname) if node else \'\'
def get_nodevalue(node, index = 0):
return node.childNodes[index].nodeValue if node else \'\'
def get_xmlnode(node, name):
return node.getElementsByTagName(name) if node else []
def get_xml_data(filename = \'user.xml\'):
doc = minidom.parse(filename)
root = doc.documentElement
user_nodes = get_xmlnode(root, \'user\')
print "user_nodes:", user_nodes
user_list=[]
for node in user_nodes:
user_id = get_attrvalue(node, \'id\')
node_name = get_xmlnode(node, \'username\')
node_email = get_xmlnode(node, \'email\')
node_age = get_xmlnode(node, \'age\')
node_sex = get_xmlnode(node, \'sex\')
user_name =get_nodevalue(node_name[0])
user_email = get_nodevalue(node_email[0])
user_age = int(get_nodevalue(node_age[0]))
user_sex = get_nodevalue(node_sex[0])
user = {}
user[\'id\'] , user[\'username\'] , user[\'email\'] , user[\'age\'] , user[\'sex\'] = (
int(user_id), user_name , user_email , user_age , user_sex
)
user_list.append(user)
return user_list
def test_load_xml():
user_list = get_xml_data()
for user in user_list :
print \'-----------------------------------------------------\'
if user:
user_str=\'No.:\\t%d\\nname:\\t%s\\nsex:\\t%s\\nage:\\t%s\\nEmail:\\t%s\' % (int(user[\'id\']) , user[\'username\'], user[\'sex\'] , user[\'age\'] , user[\'email\'])
print user_str
if __name__ == "__main__":
test_load_xml()