python解析xml文件并修改其属性值
本文章主要介绍了python 解析xml文件,修改其属性,并且进行回写的过程,博主目前发现有两种方式进行实现;但是两种实现有如下区别:
- 采用 python自带的包 xml.etree.elementtree 进行xml解析和回写,无法给xml文件添加standalone=‘yes’ 属性
- 采用 第三方包 lxml 进行xml解析和回写,可以在xml文件添加standalone=‘yes’ 属性
详细的区别请看下面的内容
一. 采用 python 自带包进行xml解析
实现给textcompent增加一个属性 status= development
- 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>
- 修改后的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>
- 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()
- 修改后的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>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论