Day015-xml module of Python module learning

Keywords: Python xml encoding Attribute

xml extends markup language, which can be used to mark data and define data types. xml is a source language that allows users to define their own markup language.

ElementTree is python's XML processing module, which provides a lightweight object model. When using the ElementTree module, you need the operation of import xml.etree.ElementTree. ElementTree represents the entire XML node tree, while Element represents a single node in the number of nodes.

XML Example 1: Use XML to read the local first.xml file and parse the data

The following is the content of the first.xml file

 1 <data>
 2     <country name="Liechtenstein">
 3         <rank updated="yes">2</rank>
 4         <year>2023</year>
 5         <gdppc>14110</gdppc>
 6         <neighbor direction="E" name="Austria"/>
 7         <neighbor direction="W" name="switzeriand"/>
 8     </country>
 9     <country name="Singapore">
10         <rank updated="yes">5</rank>
11         <year>2026</year>
12         <gdppc>59900</gdppc>
13         <neighbor direction="N" name="Malaysia"/>
14     </country>
15     <country name="Faname">
16         <rank updated="yes">69</rank>
17         <year>2019</year>
18         <gdppc>13360</gdppc>
19         <neighbor direction="W" name="Costa Rica"/>
20         <neighbor direction="E" name="Colombia"/>
21     </country>        
22 </data>
View Code

python code implementation:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 """
 4 Use XML Read local first.xml File and parse data
 5 """
 6 from xml.etree import ElementTree as ET
 7 
 8 root = ET.XML(open("first.xml", "r", encoding='utf-8').read())  #Read local first.xml File, parse string
 9 
10 #Read the content of the child node through a loop
11 for node in root:
12     print(node.tag, node.attrib, node.find("year").text)
View Code

python implements reading the first.xml file and parsing the results:

 

XML Example 2: Modifying the content of a file can be achieved by opening the file in. parse().

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 #Open and parse file contents
 5 from xml.etree import ElementTree as ET
 6 
 7 tree = ET.parse("first.xml")
 8 root = tree.getroot()  #adopt.getroot()Get the root node
 9 for node in root.iter('year'):  #adopt.iter()Iterate to find the specified child node
10     new_year = int(node.text) + 1
11     node.text = str(new_year)
12     node.set('name', 'YY')  #adopt.set()to year Add a node Name attribute
13     #del node.attrib['name']  adopt.attrib[]Deletable specified properties
14     head = root.find('gdppc')  # Getting Nodes
15     root.remove(head)                # Delete Nodes
16 
17 #adopt.write()Writing Modified Content from Memory to a File
18 tree.write("first.xml")
View Code

 

Example 3: Creating xml documents

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 from xml.etree import ElementTree as ET
 5 
 6 #Create the root node
 7 new_xml = ET.Element("namelist")
 8 
 9 #Create child node 1 under root node
10 name1 = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
11 #stay name1 Create a Sun Node under a Child Node
12 age1 = ET.SubElement(name1, "age", attrib={"checked": "no"})
13 sex1 = ET.SubElement(name1, "sex")
14 sex1.text = '1'
15 
16 #Create child node 2 under root node
17 name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
18 age2 = ET.SubElement(name2, "age")
19 age2.text = '20'
20 
21 #Generating Document Objects
22 et = ET.ElementTree(new_xml)
23 et.write("new_xml.xml", encoding='utf-8', xml_declaration=True)
View Code

The result of creating an xml document implementation:

Posted by psychosquirrel on Wed, 09 Oct 2019 09:33:33 -0700