本文章主要介绍了python 解析xml文件,修改其属性,并且进行回写的过程,博主目前发现有两种方式进行实现;但是两种实现有如下区别:
- 采用 python自带的包 xml.etree.ElementTree 进行xml解析和回写,无法给xml文件添加standalone=‘yes’ 属性
- 采用 第三方包 lxml 进行xml解析和回写,可以在xml文件添加standalone=‘yes’ 属性
详细的区别请看下面的内容
一. 采用 python 自带包进行xml解析, 实现给textCompent增加一个属性 status= development
1. xml 文件的内容如下
<?xml version='1.0' encoding='utf-8'?>
<config>
<set_status>development</set_status>
<component>
<componentElement name="occN">
<textCompent name="apk1" status="online" />
<textCompent name="apk2" status="online" />
<textCompent name="apk3" status="online" />
<textCompent name="apk4" status="online" />
<textCompent name="apk5" status="online" />
</componentElement>
<componentElement name="occK">
<textCompent name="apk1" status="online" />
<textCompent name="apk2" status="online" />
<textCompent name="apk3" status="online" />
<textCompent name="apk4" status="online" />
<textCompent name="apk5" status="online" />
</componentElement>
</component>
</config>
3.修改后的xml文件:
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<config>
<set_status>development</set_status>
<component>
<componentElement name="occN">
<textCompent name="apk1" status="development"/>
<textCompent name="apk2" status="development"/>
<textCompent name="apk3" status="development"/>
<textCompent name="apk4" status="development"/>
<textCompent name="apk5" status="development"/>
</componentElement>
<componentElement name="occK">
<textCompent name="apk1" status="development"/>
<textCompent name="apk2" status="development"/>
<textCompent name="apk3" status="development"/>
<textCompent name="apk4" status="development"/>
<textCompent name="apk5" status="development"/>
</componentElement>
</component>
</config>
2. python script xml 处理:
import xml.etree.ElementTree as ET
config_file = "demol.xml"
def change_xml():
# 解析xml文件成一个tree
tree = ET.parse(config_file)
# 获取根节点,<config>下面的内容
root = tree.getroot()
global status_set
#获取嵌入数据,例如 <set_status>development</set_status>
status_set = root.find("set_status").text
component_list = root.find("component")
for component in component_list.iter('componentElement'):
for textCompent in component.iter('textCompent'):
# 获取目前的属性值,例如 <textCompent name="apk5" status="online" />
status=textCompent.attrib.get("status")
# 设置新的属性值
textCompent.set("status",status_set)
# xml_declaration 表示带有文件头,例如 <?xml version='1.0' encoding='utf-8'?>
# 此处写文件的时候无法设置standalone属性,否则会报错
tree.write("demol.xml", encoding="utf-8",xml_declaration=True)
if __name__ == '__main__':
change_xml()
二.采用第三方包lxml进行xml的操作,实现给textCompent增加一个属性 status= development
- 下载 lxml包
pip install lxml
- python script xml 处理:
目前发现的写法区别只有:在解析xml文件和回写xml文件时有不同,其他相同
from lxml import etree
config_file = "demol.xml"
def change_xml():
# 解析xml文件成一个tree
tree = etree.parse(config_file)
# 获取根节点,<config>下面的内容
root = tree.getroot()
global status_set
#获取嵌入数据,例如 <set_status>development</set_status>
status_set = root.find("set_status").text
component_list = root.find("component")
for component in component_list.iter('componentElement'):
for textCompent in component.iter('textCompent'):
# 获取目前的属性值,例如 <textCompent name="apk5" status="online" />
status=textCompent.attrib.get("status")
# 设置新的属性值
textCompent.set("status",status_set)
# xml_declaration 表示带有文件头,例如 <?xml version='1.0' encoding='utf-8'?>
# 此处写文件的时候可以standalone属性
tree.write("demol.xml", xml_declaration=True, pretty_print=True, encoding=tree.docinfo.encoding,
standalone=tree.docinfo.standalone)
if __name__ == '__main__':
change_xml()
3.修改后的xml文件
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<config>
<set_status>development</set_status>
<component>
<componentElement name="occN">
<textCompent name="apk1" status="development"/>
<textCompent name="apk2" status="development"/>
<textCompent name="apk3" status="development"/>
<textCompent name="apk4" status="development"/>
<textCompent name="apk5" status="development"/>
</componentElement>
<componentElement name="occK">
<textCompent name="apk1" status="development"/>
<textCompent name="apk2" status="development"/>
<textCompent name="apk3" status="development"/>
<textCompent name="apk4" status="development"/>
<textCompent name="apk5" status="development"/>
</componentElement>
</component>
</config>