xml模块操作 python

前端之家收集整理的这篇文章主要介绍了xml模块操作 python前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<?xml version="1.0" encoding="UTF-8" ?>
<users>
    user id="1000001">
        username>Admin</email>admin@live.cnage>23sex>boyuser="1000002">Admin2>admin2@live.cn>22="1000003">Admin3>admin3@live.cn>27="1000004">Admin4>admin4@live.cn>25>girl="1000005">Admin5>admin5@live.cn>20="1000006">Admin6>admin6@live.cn>
>
@H_301_89@

把name、email、age、sex输出

1.参考代码

@H_301_89@
# -*- 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 def get_xmlnode(node,name): return node.getElementsByTagName(name) 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,0)">id') node_name = get_xmlnode(node,0)">username') node_email = get_xmlnode(node,0)">email') node_age = get_xmlnode(node,0)">age') node_sex = get_xmlnode(node,0)">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['],user['] = ( 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 : -----------------------------------------------------'
        if user: user_str=No.:\t%d\nname:\t%s\nsex:\t%s\nage:\t%s\nEmail:\t%s' % (int(user[']),0)">']) print user_str if __name__ == __main__": test_load_xml()
@H_301_89@

结果

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
@H_114_403@ 28
29
30
31
32
33
34
35
36
37
38
39
40
C:\Users\wzh94434\Desktop\xml>python user.py
user_nodes: [<DOM Element: user at0x2758c48>,<DOM Element: user at2756288 <DOM Element: user at27568882756e88 t: user at27548eac>]
-----------------------------------------------------
No.:1000001
name: Admin
sex: boy
age:23
Email: admin@live.cn
-----------------------------------------------------
1000002
name: Admin2
sex: boy
22
Email: admin2@live.cn
-----------------------------------------------------
1000003
3
sex: boy
27
3@live.cn
-----------------------------------------------------
1000004
4
sex: gril
25
@live.cn
-----------------------------------------------------
1000005
5
sex: boy
20
5@live.cn
-----------------------------------------------------
1000006
6
sex: gril
23
6@live.cn 
回到顶部

2.总结

28
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[]
doc = minidom.parse(filename)
doc.toxml('UTF-8')
返回Node节点的xml表示的文本
本文转载自http://www.cnblogs.com/kaituorensheng/p/4493306.html

猜你在找的XML相关文章