python模块详解 XML

时间:2021-10-19 04:21:40

 XML模块

XML是实现不同语言或程序之间进行数据交换的协议,和json一样。

XML格式:

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

 读XML

简单读取XML内容:

import xml.etree.ElementTree as ET
tree = ET.parse('xml_test.xml')
root = tree.getroot()
print(root) #XML对象
print(root.tag) #XML最外层标签

 遍历XML:

import xml.etree.ElementTree as ET
tree = ET.parse('xml_test.xml')
root = tree.getroot()

for child in root:
print(child.tag,child.attrib) #to {'name': 'to'} 遍历标签、属性
for i in child:
print(i.tag,i.text) #content Jim 遍历标签、文本
break
break

 只遍历某一个标签:

for node in root.iter('content'):
print(node.tag,node.text) #content Jim #content Jani #content Reminder #content Don't forget me this weekend!

 修改XML

for node in root.iter('content'):
content = node.text #获取内容
new_content = str('new_content_'+content)
node.text = new_content #修改内容
node.set('update','yes') #添加属性

tree.write('xml_test.xml') #保存到原文件则覆盖之前内容,保存到新文件则新建

 删除XML

for node in root.findall('to'): #找到to这个标签
content = len(node.find('content').text) #继续找到content的标签,并且计算content内容长度
if content > 2:
root.remove(node) #移除这个标签
tree.write('xml_test3.xml') # 将结果重新写到文件

创建XML

import xml.etree.ElementTree as ET
new_xml = ET.Element("name_list") #创建节点
personinfo = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"}) #创建子节点、子节点名称、属性
age = ET.SubElement(personinfo,'age',attrib={"checked":"no"})
age.text = '33' #添加子节点文本内容
name = ET.SubElement(personinfo,"name")
name.text = 'jim'

personinfo2 = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"no"})
age = ET.SubElement(personinfo2,'age')
age.text = '20'
name = ET.SubElement(personinfo2,'name')
name.text = 'Lily'

et =ET.ElementTree(new_xml) #生成文档对象
et.write('xml_test5.xml',encoding='utf-8',xml_declaration=True) #生成xml文件并指定字符集和
ET.dump(new_xml) #打印生成格式

 

创建后的结果:

<?xml version='1.0' encoding='utf-8'?>
<name_list>
<personinfo enrolled="yes">
<age checked="no">33</age>
<name>jim</name>
</personinfo>
<personinfo enrolled="no">
<age>20</age>
<name>Lily</name>
</personinfo>
</name_list>