如何使用 xml.etree.ElementTree 保存读取文件中的 XML 声明

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

我正在读取一个 xml 文件,添加一些标签并写入它。

我读到的文件有

<?xml version="1.0" encoding="UTF-8"  standalone="yes"?>
我的输出只有
<?xml version="1.0" ?>

我使用以下代码

import os
from xml.dom import minidom
import xml.etree.ElementTree as ET

    tree = ET.parse(xml_file)
    root = tree.getroot()
    access = ""

    # ... (rest of the processing logic)

    # Write to a temporary string to control indentation
    rough_string = ET.tostring(root, 'utf-8')
    reparsed = minidom.parseString(rough_string)

    # Write the formatted XML to the original file without empty lines and version information
    with open(xml_file, 'w', encoding='utf-8') as f:
        for line in reparsed.toprettyxml(indent="  ").splitlines():
            if line.strip():
                f.write(line + '\n')

如何保留原始文档中的 XML 声明?

编辑:

我通过手动添加行解决了这个问题

    with open(xml_file, 'w', encoding='utf-8') as f:
        custom_line = '<?xml version="1.0" encoding="UTF-8"  standalone="yes"?>'
        f.write(custom_line + '\n')
        for line in reparsed.toprettyxml(indent="  ").splitlines():
            if line.strip() and not line.startswith('<?xml'):
                f.write(line + '\n')
python xml elementtree
2个回答
0
投票

我认为 xml.etree.ElementTree 不支持 xml_declaration 中的独立。 使用 minidom 你可以做到这一点,例如:

from xml.dom.minidom import parseString

dom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')

# write declaration with standalone
with open("myfile.xml", "w") as xml_file:
    dom3.writexml(xml_file, indent='  ', newl='\n', encoding='utf-8', standalone=True)

给出xml声明:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
. . .

查找文档。 或者作为

xml.etree.ElementTree
的替代解决方案,您可以找到 here


0
投票

我通过添加这行解决了这个问题

with open(xml_file, 'w', encoding='utf-8') as f:
    custom_line = '<?xml version="1.0" encoding="UTF-8"  standalone="yes"?>'
    f.write(custom_line + '\n')
    for line in reparsed.toprettyxml(indent="  ").splitlines():
        if line.strip() and not line.startswith('<?xml'):
            f.write(line + '\n')
© www.soinside.com 2019 - 2024. All rights reserved.