Python parses XML the python module xml.dom parses the XML instance code

  • 2020-04-02 13:24:42
  • OfStack

I. python module xml.dom parsing XML API

Minidom. Parse (filename)
Load the read XML file

Doc. DocumentElement
Gets the XML document object

Node. The getAttribute (AttributeName)
Gets the XML node attribute value

Node. GetElementsByTagName (TagName)
Gets the collection of XML node objects

Node. childNodes # returns a list of childNodes.

Node..childnodes [index]. NodeValue
Gets the XML node value

Node. The firstChild
# access the first node. Equivalent to pagexml..childnodes [0]

Doc = minidom. Parse (filename)
Doc. Toxml () 'utf-8'
Returns the text of the XML representation of the Node Node

Node. The attributes (" id ")
A.n ame # is the "id" on the top.
The value of the a.vue # attribute
Accessing element attributes

Python parses the instance code of the XML file
1. Create user.xml file and add XML node


<?xml version="1.0" encoding="UTF-8" ?>
<users>
    <user id="1000001">
        <username>Admin</username>
        <email>admin@live.cn</email>
        <age>23</age>
        <sex> male </sex>
    </user>
    <user id="1000002">
        <username>Admin2</username>
        <email>admin2@live.cn</email>
        <age>22</age>
        <sex> male </sex>
    </user>
    <user id="1000003">
        <username>Admin3</username>
        <email>admin3@live.cn</email>
        <age>27</age>
        <sex> male </sex>
    </user>
    <user id="1000004">
        <username>Admin4</username>
        <email>admin4@live.cn</email>
        <age>25</age>
        <sex> female </sex>
    </user>
    <user id="1000005">
        <username>Admin5</username>
        <email>admin5@live.cn</email>
        <age>20</age>
        <sex> male </sex>
    </user>
    <user id="1000006">
        <username>Admin6</username>
        <email>admin6@live.cn</email>
        <age>23</age>
        <sex> female </sex>
    </user>
</users>

2. Demo.py parses user. XML document data


# -*- coding:utf-8 -*-
"""
* User: lhj588
* Date: 11-11-9
* Time: 13 : 20
* 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.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=' Ed     No. : %dn User name: %sn sex     Don't: %sn years     Age: %sn mail     Box: %sn ' % (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 effect
A. test toxml
Change to demo.py file
If __name__ = = "__main__" :
      Test_xmltostring ()

This section is part two of the python parsing XML python module xml.dom parsing an XML instance.
Execute print result:


<user id="1000001">
        <username>Admin</username>
        <email>admin@live.cn</email>
        <age>23</age>
        <sex> male </sex>
    </user>
<user id="1000002">
        <username>Admin2</username>
        <email>admin2@live.cn</email>
        <age>22</age>
        <sex> male </sex>
    </user>
<user id="1000003">
        <username>Admin3</username>
        <email>admin3@live.cn</email>
        <age>27</age>
        <sex> male </sex>
    </user>
<user id="1000004">
        <username>Admin4</username>
        <email>admin4@live.cn</email>
        <age>25</age>
        <sex> female </sex>
    </user>
<user id="1000005">
        <username>Admin5</username>
        <email>admin5@live.cn</email>
        <age>20</age>
        <sex> male </sex>
    </user>
<user id="1000006">
        <username>Admin6</username>
        <email>admin6@live.cn</email>
        <age>23</age>
        <sex> female </sex>
    </user>

B. Test parsing XML
Change to demo.py file
    If __name__ = = "__main__" :
              Test_laod_xml ()

Execute the print out result:
-----------------------------------------------------
Ed     No. : 1000001
User name: Admin
sex     Don't: male
years     Age: 23
mail     Box: admin@live.cn

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-----------------------------------------------------
Ed     No. : 1000002
User name: Admin2
sex     Don't: male
years     Age: 22
mail     Box: admin2@live.cn

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-----------------------------------------------------
Ed     No. : 1000003
User name: Admin3
sex     Don't: male
years     Age: 27
mail     Box: admin3@live.cn

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-----------------------------------------------------
Ed     No. : 1000004
User name: Admin4
sex     Don't: female
years     Age: 25
mail     Box: admin4@live.cn

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-----------------------------------------------------
Ed     No. : 1000005
User name: Admin5
sex     Don't: male
years     Age: 20
mail     Box: admin5@live.cn

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-----------------------------------------------------
Ed     No. : 1000006
User name: Admin6
sex     Don't: female
years     Age: 23
mail     Box: admin6@live.cn

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =


Related articles: