You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

xml.etree.ElementTree的indent函数缩进异常问题求助

使用xml.etree.ElementTree解决XML插入后的缩进问题

我需要将XML片段插入到基础XML结构中生成最终XML,但使用xml.etree.ElementTree时遇到了缩进问题:

  • 插入的<entity>标签未正确缩进
  • </entity>结束标签和</applicability>挤在同一行

相关代码与文件内容如下:

原Python代码

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import XMLParser

base_tree     = ET.parse('base_structure_01.XML')
base_root     = base_tree.getroot()

specification = base_tree.find('specification')
applicability = specification.find('applicability')

entity_tree   = ET.parse('entity_structure_01.XML')
entity_root   = entity_tree.getroot()

ET.indent(entity_root, space="    ", level=3)

applicability.append(entity_root)
base_tree.write('output.xml')

基础XML文件(base_structure_01.XML)

<ids>
    <specification>
        <applicability>
        </applicability>
        <requirements>
        </requirements>
    </specification>
</ids>  

待插入的XML片段(entity_structure_01.XML)

<entity>  
    <name>
        <simpleValue>name</simpleValue>
    </name>
    <predefinedtype>
        <simpleValue>predefinedtype</simpleValue>
    </predefinedtype>
</entity>

期望输出

<ids>
    <specification>
        <applicability>
            <entity>
                <name>
                    <simpleValue>name</simpleValue>
                </name>
                <predefinedtype>
                    <simpleValue>predefinedtype</simpleValue>
                </predefinedtype>
            </entity>
        </applicability>
        <requirements>
        </requirements>
    </specification>
</ids>

当前实际输出(存在缩进问题)

<ids>
    <specification>
        <applicability>
        <entity>
                <name>
                    <simpleValue>name</simpleValue>
                </name>
                <predefinedtype>
                    <simpleValue>predefinedtype</simpleValue>
                </predefinedtype>
            </entity></applicability>
        <requirements>
        </requirements>
    </specification>
</ids>

解决方案

可以通过xml.etree.ElementTree轻松解决,核心是统一处理整个XML树的缩进,而非单独处理待插入片段,并清理目标节点内的空白文本节点:

修改后的代码

import xml.etree.ElementTree as ET

base_tree = ET.parse('base_structure_01.XML')
base_root = base_tree.getroot()

specification = base_tree.find('specification')
applicability = specification.find('applicability')

# 清理applicability内部的空白文本节点(原XML中的换行、空格)
for child in list(applicability):
    if not isinstance(child, ET.Element) and child.strip() == '':
        applicability.remove(child)

entity_tree = ET.parse('entity_structure_01.XML')
entity_root = entity_tree.getroot()

applicability.append(entity_root)

# 给整个基础XML树统一设置缩进
ET.indent(base_root, space="    ")

# 写入文件时指定编码和XML声明,确保格式规范
base_tree.write('output.xml', encoding='utf-8', xml_declaration=True)

解释

  1. 清理空白文本节点:原基础XML中<applicability>标签内的换行和空格会被解析为文本节点,这些节点会干扰新插入元素的换行与缩进,所以需要先移除。
  2. 统一缩进整个树:不再单独给待插入的entity_root设置缩进,而是在插入完成后,对整个base_root执行ET.indent(),这样所有元素的缩进会保持一致,层级正确。
  3. 规范写入参数:write()方法指定encoding='utf-8'和xml_declaration=True,生成的XML文件格式更标准。

这样修改后,就能得到符合预期的缩进规范的XML输出。

内容的提问来源于stack exchange,提问作者Enrra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:26:30