import xml.etree.ElementTree as ET
xml = '''<root>
<parent attr1="foo" attr2="bar">
<child> something </child>
</parent>
</root>'''
root = ET.fromstring(xml)
parent = root.find("parent")
child = parent.find("child")
child.attrib = parent.attrib
root.append(child)
root.remove(parent)
# next code is just to print patched XML
ET.indent(root)
ET.dump(root)
1条答案
按热度按时间qyswt5oh1#
那么,您需要找到
<parent>
,然后找到<child>
,将属性从<parent>
复制到<child>
,将<child>
附加到根节点并删除<parent>
。结果: