Python has three methods to parse XML, sax, Dom, and elementtree.
### 1.sax (Simple API for XML)
The standard pyhton library contains a SAX Parser. Sax is a typical extremely fast tool that does not occupy a large amount of memory when parsing XML.
But this is based on the callback mechanism, so in some data, it will call some methods for transmission. This means that you must specify a handle for the data,
It is very difficult to maintain your own status.
### 2.dom( Document Object Model)
Compared with sax, the typical disadvantage of Dom is that it is slow and consumes more memory, because Dom reads the entire XML number into the memory and
The first node in creates an object. The advantage of using Dom is that you do not need to track the status, because every node knows who it is.
Parent node, who is a child node. However, Dom is a little difficult to use.
### 3. elementtree (element tree)
Elementtree is like a lightweight Dom with convenient and friendly APIs.CodeHigh Availability, fast speed, and low memory consumption.
This section describes elementtree.
The following is a reprinted example:
Test. XML is as follows:
[HTML] View plaincopy
- <Span Style="Font-size: 13px ;"><?XML Version="1.0" Encoding="UTF-8"?>
- <Root>
- <Person Age="18">
- <Name>Hzj</Name>
- <Sex>Man</Sex>
- </Person>
- <Person Age="19" Des="Hello">
- <Name>Kiki</Name>
- <Sex>Female</Sex>
- </Person>
- </Root></Span>
1. load XML files
There are two ways to load an XML file: loading a specified string and loading a specified file.
2. Obtain the Element Method
A) through getiterator
B) getchildren
C) Find Method
D) findall Method
[Python] View plaincopy
- <Span style ="Font-size: 13px ;">#-*-Coding: UTF-8 -*-
- FromXML. etreeImportElementtree
- DefPrint_node (node ):
- '''''Print basic node information '''
- Print "============================================== ======="
- Print "Node. attrib: % s"% Node. attrib
- IfNode. attrib. has_key ("Age")>0:
- Print "Node. attrib ['age']: % s"% Node. attrib ['Age']
- Print "Node. Tag: % s"% Node. Tag
- Print "Node. Text: % s"% Node. Text
- DefRead_xml (text ):
- '''''Read XML file '''
- # Loading XML files (two methods: loading a specified string and loading a specified file)
- # Root = elementtree. parse (R "D:/test. xml ")
- Root = elementtree. fromstring (text)
- # Obtain the Element Method
- #1 through getiterator
- Lst_node = root. getiterator ("Person")
- ForNodeInLst_node:
- Print_node (node)
- #2 Use getchildren
- Lst_node_child = lst_node [0]. Getchildren ()[0]
- Print_node (lst_node_child)
- #3. Find Method
- Node_find = root. Find ('Person')
- Print_node (node_find)
- #4. findall Method
- Node_findall = root. findall ("Person/name")[1]
- Print_node (node_findall)
- If_ Name _ ='_ Main __':
- Read_xml (open ("Test. xml"). Read ())
- </Span>
Think about why? Do not understand, please refer to the following
[Python] View plaincopy
- # Encoding = UTF-8
- FromXML. etreeImportElementtree as et
- # Identify the age of all people
- Per = ET. parse ('Test. xml')
- P = per. findall ('/Person')
- ForXInP:
- PrintX. attrib
- Print
- ForOneperInP:# Finding the person Node
- ForChildInOneper. getchildren ():# Find the child node of the person Node
- PrintChild. Tag,':', Child. Text
- Print 'Age :', Oneper. Get ('Age')
- Print '############'
The result is as follows:
[Python] View plaincopy
- {'Age':'18'}
- {'Age':'19','Des':'Hello'}
- Name: hzj
- Sex: Man
- Age:18
- ############
- Name: Kiki
- Sex: Female
- Age:19
- ############