I. XML content
<data>
<country name= "Liechtenstein" >
<rank updated= "Yes" >2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction= "E" name= "Austria"/>
<neighbor direction= "W" Name= "Switzerland"/>
</country>
<country name= "Singapore" >
<rank updated= "Yes" >5</rank>
<year>2026</year>
<gdppc>59900</gdppc>
<neighbor direction= "N" name= "Malaysia"/>
</country>
<country name= "Panama" >
<rank updated= "Yes" >69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction= "W" name= "Costa Rica"/>
<neighbor direction= "E" name= "Colombia"/>
</country>
</data>
1. Parsing XML
From Xml.etree import ElementTree as ET
#打开XML, read the contents of the XML
Str_xml = open ("Fisrt1.xml", ' R '). Read ()
#将字符串解析成XML对象
root = ET. XML (Str_xml)
2. Parsing XML documents directly
#直接解析XML文件
Str_xml = Et.parse ("Fisrt1.xml")
#获取XML文件的根节点
root = Str_xml.getroot ()
2. Manipulating XML files
A. Traversing all content in an XML document
From Xml.etree import ElementTree as ET
#直接解析XML文件
Str_xml = Et.parse ("Fisrt1.xml")
#获取XML文件的根节点
root = Str_xml.getroot ()
#顶层标签
Print (Root.tag)
#遍历XML文件的第二层标签
For child in Root:
Print (Child.tag, child.attrib)
#遍历XML文件的第三层标签
For Sub_child in child:
Print (Sub_child.tag, sub_child.text)
B. Traversing a pointing node in an XML file
From Xml.etree import ElementTree as ET
#打开XML, read the contents of the XML
#直接解析XML文件
Str_xml = Et.parse ("Fisrt1.xml")
#获取XML文件的根节点
root = Str_xml.getroot ()
#顶层标签
Print (Root.tag)
For node in Root.iter ("year"):
Print (Node.tag, node.text)
For node in Root.iter ("GDPPC"):
Print (Node.tag, node.text)
C. Modifying the contents of a node
Because the content of the node is modified in memory, it does not affect the contents of the file. If you want to modify the contents of a file successfully, you need to save the result of the memory modification to a file
From Xml.etree import ElementTree as ET
#直接解析XML文件
Str_xml = Et.parse ("Fisrt1.xml")
#获取XML文件的根节点
root = Str_xml.getroot ()
#顶层标签
Print (Root.tag)
#修改节点的内容
For node in Root.iter ("year"):
#将节点的内容自加1
new_year = Int (node.text) + 1
Node.text = str (new_year)
#设置节点
Node.set (' name ', ' HJW ')
Node.set (' Age ', ' 18 ')
# Save the modified file
Tree = ET. ElementTree (Root)
Tree.write (' aa.xml ', encoding = ' utf-8 ')
D. Deleting a node
from Xml.etree import elementtree as ET
#直接解析XML文件
Str_xml = Et.parse ("Fisrt1.xml")
# Gets the root node of the xml file
root = Str_xml.getroot ()
#顶层标签
Print (root.tag)
#遍历data下的country下节点
for Country In Root.findall (' Country '):
#获取每个rank节点的内容
rank = Int (country.find (' rank '). Text)
If rank > 50:
#删除指定的country节点
Root.remove (country)
#保存修改后的内容
tree = ET. ElementTree (Root)
Tree.write (' Bb.xml ', encoding= ' utf-8 ')
3. Create XML
From Xml.etree import ElementTree as ET
#创建根节点
root = ET. Element (' famliy ')
#创建节点大儿子
Son1 = ET. Element (' son ', {' name ': ' Child 1 '})
#创建节点小儿子
Son2 = ET. Element (' son ', {' name ': ' Child 2 '})
#在大儿子中创建孙子节点
Grandson1 = ET. Element (' grandson ', {' name ': ' Child 11 '})
Grandson2 = ET. Element ("grandson", {' name ': ' Child 12 '})
#把孙子添加到儿1中
Son1.append (Grandson1)
Son1.append (Grandson2)
#把儿子添加中根节点中
Root.append (Son1)
Root.append (Son2)
#b保存文件
Tree = ET. ElementTree (Root)
Tree.write (' new_son1.xml ', encoding = ' utf-8 ')
Python Basic Knowledge value XML