There are a lot of reading XML articles about Python, but most articles post an XML file and then paste the code that handles the file. This is not conducive to the learning of beginners, I hope this article can be more understandable to teach how to use Python to read XML files.
First, what is XML?
XML Extensible Markup Language, which can be used to tag data, define data types, is a source language that allows users to define their own markup language.
Abc.xml
The code is as follows:
4
Python
Test
Zope
Ok, structurally, it's much like our common HTML Hypertext Markup Language. But they are designed to be different, Hypertext Markup language is designed to display data, and its focus is on the appearance of the data. It is designed to transmit and store data, with the focus on the content of the data.
Then it has the following characteristics:
First of all, it's got a label on the composition,
Tags can have attributes:
Tag pairs can embed data: ABC
Tags can embed sub-tags (with hierarchical relationships):
Second, get tag attributes
So, here's how to read this type of file in Python.
The code is as follows:
#coding =utf-8
Import Xml.dom.minidom
#打开xml文档
Dom = Xml.dom.minidom.parse (' Abc.xml ')
#得到文档元素对象
root = Dom.documentelement
Print Root.nodename
Print Root.nodevalue
Print Root.nodetype
Print root. Element_node
The Mxl.dom.minidom module is used to process XML files, so it should be introduced first.
Xml.dom.minidom.parse () is used to open an XML file and will be the DOM variable for this file object.
DocumentElement is used to get the document elements of the DOM object and to give the obtained object to root
Each node has its Nodename,nodevalue,nodetype property.
NodeName is the node name.
NodeValue is the value of the node and is valid only for text nodes.
NodeType is the type of node. Catalog is a Element_node type
Now there are the following types:
' Attribute_node '
' Cdata_section_node '
' Comment_node '
' Document_fragment_node '
' Document_node '
' Document_type_node '
' Element_node '
' Entity_node '
' Entity_reference_node '
' Notation_node '
' Processing_instruction_node '
' Text_node '
Third, get the sub-label
Now you want to get the label for the child tag of catalog name
The code is as follows:
4
Python
Test
Zope
For child elements that know the name of an element, you can use the getElementsByTagName method to obtain:
The code is as follows:
#coding =utf-8
Import Xml.dom.minidom
#打开xml文档
Dom = Xml.dom.minidom.parse (' Abc.xml ')
#得到文档元素对象
root = Dom.documentelement
bb = Root.getelementsbytagname (' Maxid ')
b= Bb[0]
Print B.nodename
bb = root.getelementsbytagname (' login ')
b= Bb[0]
Print B.nodename
How to differentiate labels for the same tag name:
The code is as follows:
4
Python
Test
Zope
and label more than one how to differentiate?
The code is as follows:
#coding =utf-8
Import Xml.dom.minidom
#打开xml文档
Dom = Xml.dom.minidom.parse (' Abc.xml ')
#得到文档元素对象
root = Dom.documentelement
bb = root.getelementsbytagname (' caption ')
b= Bb[2]
Print B.nodename
bb = Root.getelementsbytagname (' item ')
b= Bb[1]
Print B.nodename
Root.getelementsbytagname (' caption ') is labeled caption A set of labels, b[0] represents the first of a set of labels, and B[2], which represents the third of this set of labels.
Iv. obtaining the value of the tag attribute
The code is as follows:
4
Python
Test
Zope
and tags are properties, how do you get their properties?
The code is as follows:
#coding =utf-8
Import Xml.dom.minidom
#打开xml文档
Dom = Xml.dom.minidom.parse (' Abc.xml ')
#得到文档元素对象
root = Dom.documentelement
itemlist = root.getelementsbytagname (' login ')
item = itemlist[0]
Un=item.getattribute ("username")
Print UN
Pd=item.getattribute ("passwd")
Print PD
II = root.getelementsbytagname (' item ')
I1 = ii[0]
I=i1.getattribute ("id")
Print I
I2 = ii[1]
I=i2.getattribute ("id")
Print I
The GetAttribute method can get the value corresponding to the attribute of the element.
V. Obtaining data between label pairs
The code is as follows:
4
Python
Test
Zope
There is data between the pair of tags, how to get the data?
There are several ways to get the data between tag pairs,
Method One:
The code is as follows:
#coding =utf-8
Import Xml.dom.minidom
#打开xml文档
Dom = Xml.dom.minidom.parse (' Abc.xml ')
#得到文档元素对象
root = Dom.documentelement
Cc=dom.getelementsbytagname (' caption ')
C1=CC[0]
Print C1.firstChild.data
C2=CC[1]
Print C2.firstChild.data
C3=CC[2]
Print C3.firstChild.data
The FirstChild property returns the first child node of the selected node, and. Data indicates that the node person is fetched.
Method Two:
The code is as follows:
#coding =utf-8
From Xml.etree import ElementTree as ET
Per=et.parse (' Abc.xml ')
P=per.findall ('./login/item ')
For Oneper in P:
For child in Oneper.getchildren ():
Print Child.tag, ': ', Child.text
P=per.findall ('./item ')
For Oneper in P:
For child in Oneper.getchildren ():
Print Child.tag, ': ', Child.text
The method two is somewhat complex, and the referenced module is not the same as the previous one, and FindAll is used to specify which level of tab to start the traversal.
The GetChildren method returns all child tags in document order. and output tag name (Child.tag) and label data (Child.text)
In fact, the role of method two does not lie in this, its core function is to traverse a certain level of tags under all sub-tags.