This article mainly introduces the example code for parsing xml files using the xml. dom module in python. learn how to parse xml files using python. For more information, see
I. python module xml. dom parsing XML API
Minidom. parse (filename)
Load and read XML files
Doc.doc umentElement
Get XML document objects
Node. getAttribute (AttributeName)
Get XML node attribute values
Node. getElementsByTagName (TagName)
Get XML node object set
Node. childNodes # return to the subnode list.
Node. childNodes [index]. nodeValue
Get XML node value
Node. firstChild
# Access the first node. Equivalent to pagexml. childNodes [0]
Doc = minidom. parse (filename)
Doc. toxml ('utf-8 ')
Returns the xml representation of the Node.
Node. attributes ["id"]
A. name # Is the above "id"
A. value # attribute value
Access element attributes
II. example code for parsing xml files using python
1. create a user. xml file and add an XMl node.
The code is as follows:
Admin
Admin@live.cn
23
Male
Admin2
Admin2@live.cn
22
Male
Admin3
Admin3@live.cn
27
Male
Admin4
Admin4@live.cn
25
Female
Admin5
Admin5@live.cn
20
Male
Admin6
Admin6@live.cn
23
Female
2. Demo. py parses user. xml document data
The code is as follows:
#-*-Coding: UTF-8 -*-
"""
* User: lhj588
* Date: 11-11-9
* Time:
* Desc:
"""
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 xml_to_string (filename = 'User. XML '):
Doc = minidom. parse (filename)
Return doc. toxml ('utf-8 ')
Def get_xml_data (filename = 'User. XML '):
Doc = minidom. parse (filename)
Root = doc.doc umentElement
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 = 'code: % d \ n username: % s \ n sex: % s \ n age: % s \ n mail box: % 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 ()
3. test results
A. test toxml
Modify the demo. py file
If _ name _ = "_ main __":
Test_xmltostring ()
This section describes how to use python to parse the second part of the XML python module xml. dom to parse the xml instance.
Print the result:
The code is as follows:
Admin
Admin@live.cn
23
Male
Admin2
Admin2@live.cn
22
Male
Admin3
Admin3@live.cn
27
Male
Admin4
Admin4@live.cn
25
Female
Admin5
Admin5@live.cn
20
Male
Admin6
Admin6@live.cn
23
Female
B. test parsing XML
Modify the demo. py file
If _ name _ = "_ main __":
Test_laod_xml ()
Run the following command to print the result:
-----------------------------------------------------
Code: 1000001
Username: Admin
Sex: Male
Age: 23
Mail: admin@live.cn
========================================================== ==================
-----------------------------------------------------
Code: 1000002
Username: Admin2
Sex: Male
Age: 22
Mail: admin2@live.cn
========================================================== ==================
-----------------------------------------------------
Code: 1000003
Username: Admin3
Sex: Male
Age: 27
Mail: admin3@live.cn
========================================================== ==================
-----------------------------------------------------
Code: 1000004
Username: Admin4
Sex: Female
Age: 25
Mail: admin4@live.cn
========================================================== ==================
-----------------------------------------------------
Code: 1000005
Username: Admin5
Sex: Male
Age: 20
Mail: admin5@live.cn
========================================================== ==================
-----------------------------------------------------
Code: 1000006
Username: Admin6
Sex: Female
Age: 23
Mail: admin6@live.cn
========================================================== ==================