嗨,我有一种情况,我需要通过xml解析并找到其所有对应的子代,并根据它们所属的标记来设置父代ID。
请找到xml
<response success="true">
<output>
<levels seqNo="5069">
<level id="1" isLinked="0" hasChildren="true">
<level id="1051" isLinked="0" hasChildren="true">
<level id="951" isLinked="0" hasChildren="true">
<level id="1073" isLinked="0" hasChildren="true">
<level id="922" isElimination="0" isLinked="0" />
<level id="957" isLinked="0" />
<level id="1301" isLinked="0">
<level id="927" isLinked="0" />
<level id="939" isLinked="0" />
</level>
<level id="1302" isLinked="0" hasChildren="true">
<level id="925" isLinked="0" />
</level>
</level>
</level>
</level>
</level>
</levels>
</output>
</response>
我尝试下面的代码提取所有子代,但不确定如何设置父代ID
tree = et.parse(r"responseLevels.xml")
root = tree.getroot()
print(root)
for i in root.iter():
for j in list(i):
print(j.tag,j.attrib)
我正在寻找的输出:
id islinked haschildren Parent
1 0 "true"
1051 0 "true" 1
951 0 "true" 1051
1073 0 "true" 951
922 0 1073
957 0 1073
1301 0 1073
927 0 1301
939 0 1301
1302 0 "true" 1073
925 0 1302