1 回答
TA贡献1836条经验 获得超5个赞
你不需要深度复制任何东西;只需将EESSN元素分配给一个变量并将其附加到Dependent.
XML 输入
<Company>
<Identifier>Benesolv</Identifier>
<Employee>
<EESSN>111111111</EESSN>
<Dependent>
<SSN>222222222</SSN>
</Dependent>
</Employee>
</Company>
Python代码
import xml.etree.ElementTree as ET
tree = ET.parse("input.xml")
for employee in tree.findall("Employee"):
eessn = employee.find("EESSN")
for dependent in employee.findall("Dependent"):
dependent.append(eessn)
tree.write("output.xml")
XML 输出
<Company>
<Identifier>Benesolv</Identifier>
<Employee>
<EESSN>111111111</EESSN>
<Dependent>
<SSN>222222222</SSN>
<EESSN>111111111</EESSN>
</Dependent>
</Employee>
</Company>
添加回答
举报