python解析xml文件并修改其属性值方式

 更新时间:2025年02月19日 15:55:28   作者:爱吃面条的小白  
本文介绍了两种解析和修改XML文件的方法:使用Python自带的xml.etree.ElementTree和第三方的lxml,lxml方法可以添加standalone属性,而ElementTree则不能

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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python基础进阶之海量表情包多线程爬虫功能的实现

    Python基础进阶之海量表情包多线程爬虫功能的实现

    这篇文章主要介绍了Python基础进阶之海量表情包多线程爬虫,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • Python字符串对齐、删除字符串不需要的内容以及格式化打印字符

    Python字符串对齐、删除字符串不需要的内容以及格式化打印字符

    这篇文章主要给大家介绍了关于Python字符串对齐、删除字符串不需要的内容以及格式化打印字符的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • python关于矩阵重复赋值覆盖问题的解决方法

    python关于矩阵重复赋值覆盖问题的解决方法

    这篇文章主要介绍了python关于矩阵重复赋值覆盖问题的解决方法,涉及Python深拷贝与浅拷贝相关操作与使用技巧,需要的朋友可以参考下
    2019-07-07
  • 1 行 Python 代码快速实现 FTP 服务器

    1 行 Python 代码快速实现 FTP 服务器

    FTP 服务器,在此之前我都是使用Linux的vsftpd软件包来搭建FTP服务器的,现在发现了利用pyftpdlib可以更加简单的方法即可实现FTP服务器的功能。下面小编给大家带来了1 行 Python 代码快速实现 FTP 服务器,需要的朋友参考下
    2018-01-01
  • Python语言中的Selenium环境搭建

    Python语言中的Selenium环境搭建

    本文主要介绍了Python语言中的Selenium环境搭建,Python+Selenium这篇文章将不断的持续更新和重构,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • opencv 实现特定颜色线条提取与定位操作

    opencv 实现特定颜色线条提取与定位操作

    这篇文章主要介绍了opencv 实现特定颜色线条提取与定位操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • python MD5加密的示例

    python MD5加密的示例

    这篇文章主要介绍了python MD5加密的示例,帮助大家更好的利用python进行加密,感兴趣的朋友可以了解下
    2020-10-10
  • python识别图像并提取文字的实现方法

    python识别图像并提取文字的实现方法

    这篇文章主要介绍了python识别图像并提取文字的实现方法,
    2019-06-06
  • Python自然语言处理库之NLTK库初级教程

    Python自然语言处理库之NLTK库初级教程

    NLTK(Natural Language Toolkit)是一个Python库,用于实现自然语言处理(NLP)的许多任务,NLTK包括一些有用的工具和资源,如文本语料库、词性标注器、语法分析器等,在这篇初级教程中,我们将了解NLTK的基础功能,需要的朋友可以参考下
    2023-08-08
  • 在Django中动态地过滤查询集的实现

    在Django中动态地过滤查询集的实现

    本文主要介绍了Django中动态地过滤查询集的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论