Python xml
第一部分:读
########
##
# -*- coding:utf-8 -*-
"""
* User: not me
* Date: 11-11-9
* Time: 13:20
* Desc: not easy for newer like me
"""
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_r(name) if node else []
def xml_to_string(filename='example.xml'):
doc = minidom.parse(filename)
return doc.toxml('UTF-8')
def get_xml_data(filename='example.xml'):
doc = minidom.parse(filename)
root = doc.documentElement
user_nodes = get_xmlnode(root,'user')
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]).encode('utf-8','ignore')
user_email = get_nodevalue(node_email[0]).encode('utf-8','ignore')
user_age = int(get_nodevalue(node_age[0]))
user_sex = get_nodevalue(node_sex[0]).encode('utf-8','ignore')
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_xmltostring():
print xml_to_string()
def test_laod_xml():
user_list = get_xml_data()
for user in user_list :
#print user['sex']
print '-----------------------------------------------------'
if user:
user_str='编 号:%d\n用户名:%s\n性 别:%s\n年 龄:%s\n邮 箱:%s\n ' % (int(user['id']) , user['username'], user['sex'] , user['age'] , user['email'])
print user_str
print '====================================================='
if __name__ == "__main__":
test_xmltostring()
test_laod_xml()
##code end
############
doc=minidom.parse('example.xml')
root=doc.documentElement
root.getElementsByTagName_r('user')[0].getAttribute('id')
obtain the id attribute
<user id="1000001">
root.getElementsByTagName_r('user')[0].getElementsByTagName_r('username')[0].chileNodes[0].nodeValue
get nodevalue not attribute
<username>Admin</username>
creatDocument()方法可以创建一个指定类型的XML文档对象
##note section
##############
<?xml version="1.0" encoding="UTF-8" ?>
<users>
<user id="1000001">
<username>Admin</username>
<email>admin@live.cn</email>
<age>23</age>
<sex>男</sex>
</user>
</users>
##exampl.xml
###############
第二部分:写
from xml.dom import minidom, Node
doc = minidom.Document()
doc.a(doc.createComment("Simple xml document__chapter 8"))
doc = minidom.Document()
doc.a(doc.createComment("Simple xml document__chapter 8"))
#generate the book
#<book> (root node)
book = doc.createElement_x_x('book')
doc.a(book)
doc.a(book)
#the title
#<title>
# sample xml thing
#</title>
title = doc.createElement_x_x('title')
title.a(doc.createTextNode("sample xml thing"))
book.a(title) #在title在book下
# sample xml thing
#</title>
title = doc.createElement_x_x('title')
title.a(doc.createTextNode("sample xml thing"))
book.a(title) #在title在book下
任何级别的元素都有doc.creatElement('string')产生,但由自己的上级node执行
#the author section
<book>
<title>
sample xml thing
</title>
<author>
<name>
<first>
ma
</first>
<last>
xiaoju
</last>
</name>
author = doc.createElement_x("author")
book.a(author)
name = doc.createElement_x('name')
author.a(name)
firstname = doc.createElement_x('first')
firstname.a(doc.createTextNode("ma"))
name.a(firstname)
lastname = doc.createElement_x('last')
name.a(lastname)
lastname.a(doc.createTextNode("xiaoju"))
sample xml thing
</title>
<author>
<name>
<first>
ma
</first>
<last>
xiaoju
</last>
</name>
author = doc.createElement_x("author")
book.a(author)
name = doc.createElement_x('name')
author.a(name)
firstname = doc.createElement_x('first')
firstname.a(doc.createTextNode("ma"))
name.a(firstname)
lastname = doc.createElement_x('last')
name.a(lastname)
lastname.a(doc.createTextNode("xiaoju"))