当前位置: 代码网 > it编程>前端脚本>Python > python解析xml文件并修改其属性值方式

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

2025年02月19日 Python 我要评论
python解析xml文件并修改其属性值本文章主要介绍了python 解析xml文件,修改其属性,并且进行回写的过程,博主目前发现有两种方式进行实现;但是两种实现有如下区别:采用 python自带的包

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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com