如何在Python中替换xml节点值,而不更改整个文件

问题描述 投票:0回答:1

在 python 中执行第一步时,我尝试解析并更新 xml 文件。 xml如下:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="util/style/aaaa-2-0.xsl" type="text/xsl"?>
<test dtd-version="3.2" xmlns:test="http://www.ich.org/test" xmlns:xlink="http://www.w3c.org/1999/xlink">
  <mr>
    <leaf  checksum="88ed245997a341a4c7d1e40d614eb14f"   >
      <title>book name</title>
    </leaf>
  </mr>
</test>

我想更新校验和的值。我已经用一种方法编写了一个类:

    @staticmethod
    def replace_checksum_in_index_xml(xml_file_path, checksum):
        logging.debug(f"ReplaceChecksumInIndexXml xml_file_path: {xml_file_path}")
        try:
            from xml.etree import ElementTree as et
            tree = et.parse(xml_file_path)
            tree.find('.//leaf').set("checksum", checksum)
            tree.write(xml_file_path)
        except Exception as e:
            logging.error(f"Error updating checksum in {xml_file_path}: {e}")

我调用该方法:

    xml_file_path = "index.xml"
    checksum = "aaabbb"
    Hashes.replace_checksum_in_index_xml(xml_file_path, checksum)

校验和确实已更新。而且整个 xml 结构也发生了变化:

<test dtd-version="3.2">
  <mr>
    <leaf checksum="aaabbb">
      <title>book name</title>
    </leaf>
  </mr>
</test>

如何仅更新给定节点,而不更改给定 xml 文件中的其他任何内容?

python xml xpath elementtree
1个回答
0
投票

不幸的是,我还没有找到使用您正在使用的库 xml.etree 的解决方案。相反,请考虑使用 lxml。以下对我有用:

class Hashes:
    @staticmethod
    def replace_checksum_in_index_xml(xml_file_path, checksum):
        try:
            from lxml import etree
            tree = etree.parse(xml_file_path)
            
            leaf = tree.find('.//leaf')
            if leaf is not None:
                leaf.set("checksum", checksum)

            with open(xml_file_path, 'wb') as file:
                tree.write(file, xml_declaration=True, encoding='utf-8', pretty_print=False)
        except Exception as e:
            print(f"Error updating checksum in {xml_file_path}: {e}")
© www.soinside.com 2019 - 2024. All rights reserved.