python ElementTree操作xml节点

python ElementTree操作xml节点,包括增删改查

xml原文

<Voucher>
    <Id>967a198783d14835860574c697478156</Id>
    <Remark>main摘要443344245567583384475</Remark>
    <Delete>需要删除的节点1</Delete>
    <DetailList>
        <Detail>
            <Id>f0bfdbad7de14d1386c6f7fee6c9fa66</Id>
            <Remark>摘要443344245567583384475</Remark>
            <Delete>需要删除的节点2</Delete>
        </Detail>
        <Detail>
            <Id>6f9a8722e78f455b953ac20161736499</Id>
            <Remark>摘要8476127329416243421762023</Remark>
            <Delete>需要删除的节点3</Delete>
        </Detail>
    </DetailList>
</Voucher>

要求对这个xml 进行如下操作:

修改Voucher/Remark、Voucher/DetailList/Detail/Remark值,在原有值后面后面增加2023
增加Voucher/AdmDivCode、Voucher/DetailList/Detail/AdmDivCode,值为440300000
删除节点:Voucher/Delete、Voucher/DetailList/Detail/Delete
 
import xml.etree.ElementTree as ET
"""
修改Voucher/Remark、Voucher/DetailList/Detail/Remark值,在原有值后面后面增加2023
增加Voucher/AdmDivCode、Voucher/DetailList/Detail/AdmDivCode,值为440300000
删除节点:Voucher/Delete、Voucher/DetailList/Detail/Delete
"""
# 创建XML字符串
xml_data = """<Voucher>
    <Id>967a198783d14835860574c697478156</Id>
    <Remark>main摘要443344245567583384475</Remark>
    <Delete>需要删除的节点1</Delete>
    <DetailList>
        <Detail>
            <Id>f0bfdbad7de14d1386c6f7fee6c9fa66</Id>
            <Remark>摘要443344245567583384475</Remark>
            <Delete>需要删除的节点2</Delete>
        </Detail>
        <Detail>
            <Id>6f9a8722e78f455b953ac20161736499</Id>
            <Remark>摘要8476127329416243421762023</Remark>
            <Delete>需要删除的节点3</Delete>
        </Detail>
    </DetailList>
</Voucher>
"""

# 解析XML,root的tag就是根节点Voucher
root = ET.fromstring(xml_data)

# 修改Voucher/Remark值
voucher_remark = root.find('Remark')
voucher_remark.text += '=====2023'

# 修改Voucher/DetailList/Remark值
for detail in root.findall('.//Detail/Remark'):
    detail.text += '=====2023'

# 一次性批量修改
for node in root.findall(".//Remark"):
    node.text += '=====2023+++++'

# 添加Voucher/AdmDivCode
adm_div_code = ET.Element('AdmDivCode')
adm_div_code.text = '440300000++++++'
root.append(adm_div_code)

# 添加Voucher/DetailList/AdmDivCode
for detail in root.findall('.//Detail'):
    adm_div_code = ET.Element('AdmDivCode')
    adm_div_code.text = '440300000==='
    detail.append(adm_div_code)

#删除根目录下节点Delete :Voucher/Delete
delete_element = root.find('Delete')
root.remove(delete_element)

# 删除Voucher/DetailList/Detail目录下Delete
for detail in root.findall(".//DetailList/Detail"):
    # 删除只有先进入父目录,在以父目录身份删除子节点
    for node in detail.findall("Delete"):
        detail.remove(node)

# 将修改后的XML转换为字符串并打印
modified_xml = ET.tostring(root, encoding='utf-8').decode()
print(modified_xml)

执行结果如下,增删该都成功了

<Voucher>
    <Id>967a198783d14835860574c697478156</Id>
    <Remark>main摘要443344245567583384475=====2023=====2023+++++</Remark>
    <DetailList>
        <Detail>
            <Id>f0bfdbad7de14d1386c6f7fee6c9fa66</Id>
            <Remark>摘要443344245567583384475=====2023=====2023+++++</Remark>
            <AdmDivCode>440300000===</AdmDivCode></Detail>
        <Detail>
            <Id>6f9a8722e78f455b953ac20161736499</Id>
            <Remark>摘要8476127329416243421762023=====2023=====2023+++++</Remark>
            <AdmDivCode>440300000===</AdmDivCode></Detail>
    </DetailList>
<AdmDivCode>440300000++++++</AdmDivCode></Voucher>

 xml 操作模块ElementTree说明文档:https://docs.python.org/3/library/xml.etree.elementtree.html

 

posted @ 2023-11-08 15:40  testway  阅读(6)  评论(0编辑  收藏  举报