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

Python递归拼接XML层级路径并提取对应值的实现求助

XML层级路径拼接与元素解析解决方案

本人是Python新手,需要拼接包含子级的XML路径,并根据该路径解析对应元素。给定如下示例XML结构,期望生成指定格式的层级路径与对应值的输出。尝试过getpath、find等方法但未成功,寻求解决方案。

示例XML

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<File>
    <Root>
        <Group>
            <Name>Data</Name>
            <Entry>
                <String>
                    <Key>Name</Key>
                    <Value>Michael</Value>
                </String>
            </Entry>
            <Group>
                <Name>SUB</Name>
                <Entry>
                    <String>
                        <Key>Name</Key>
                        <Value>Vanessa</Value>
                    </String>
                </Entry>
                <Group>
                    <Name>SUB_SUB</Name>
                    <Entry>
                        <String>
                            <Key>Name</Key>
                            <Value>Ernie</Value>
                        </String>
                    </Entry>
                    <Group>
                        <Name>SUB_SUB_SUB</Name>
                        <Entry>
                            <String>
                                <Key>Name</Key>
                                <Value>Paul</Value>
                            </String>
                        </Entry>
                    </Group>
                </Group>
            </Group>
            <Group>
                <Name>SUB2</Name>
                <Entry>
                    <String>
                        <Key>Name</Key>
                        <Value>Peter</Value>
                    </String>
                </Entry>
                <Group>
                    <Name>SUB_SUB2</Name>
                    <Entry>
                        <String>
                            <Key>Name</Key>
                            <Value>Klaus</Value>
                        </String>
                    </Entry>
                </Group>
            </Group>
        </Group>
        
    </Root>
</File>

期望输出

File\Root\Data Name=Michael
File\Root\Data\SUB Name=Vanessa
File\Root\Data\SUB_SUB Name=Ernie
File\Root\Data\SUB_SUB\SUB_SUB_SUB Name=Paul
File\Root\Data\SUB2 Name=Peter
File\Root\Data\SUB2\SUB_SUB2 Name=Klaus

解决方案代码

使用Python内置的xml.etree.ElementTree模块,通过递归遍历Group节点实现路径拼接与元素解析:

import xml.etree.ElementTree as ET

def traverse_groups(group_node, current_path):
    # 获取当前Group的名称
    group_name = group_node.find('Name').text
    # 拼接完整路径
    full_path = f"{current_path}\\{group_name}" if current_path else group_name
    # 提取当前Group对应的Value值
    value = group_node.find('.//Entry/String/Value').text
    # 按指定格式输出
    print(f"{full_path} Name={value}")
    
    # 递归处理所有子Group
    for sub_group in group_node.findall('Group'):
        traverse_groups(sub_group, full_path)

# 解析XML文件(如果是XML字符串,可替换为ET.fromstring(xml_str))
tree = ET.parse('your_xml_file.xml')
root = tree.getroot()

# 从初始路径File\Root开始,遍历第一个Group节点
initial_path = "File\\Root"
first_group = root.find('Root/Group')
traverse_groups(first_group, initial_path)

代码说明

  • 递归遍历:traverse_groups函数递归处理每个Group节点,每次将当前Group的名称追加到路径中。
  • 元素定位:用find方法直接获取当前Group的名称和对应的Value值,.//Entry/String/Value可精准定位到当前Group下的目标元素。
  • 路径拼接:初始路径固定为File\Root,每进入一层子Group就自动扩展路径。

运行代码后即可得到符合要求的输出格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 16:21:01