【发布时间】:2019-06-03 11:30:33
【问题描述】:
我有这样的代码,用于在 root_compare 函数中从 xml 中删除所有名为 sysId 的标签:
#removing sysId from comparison
for rm1 in xml_root1.findall('.//sysId'):
xml_root1.remove(rm1)
代码给了我这个错误:
File "/tmp/dev_uac_api2/uac_api_lib.py", line 105, in root_compare
xml_root1.remove(rm1)
File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 337, in remove
self._children.remove(element)
ValueError: list.remove(x): x not in list
我需要遍历 xml 中的所有元素,甚至是子元素、孙子元素,并删除名为 sysId 的元素。你能帮我解决这个问题吗?
xml 结构类似于:
<root>
<sysId></sysId>
<b></b>
<c>
<sysId></sysId>
</c>
<d>
<e>
<sysId></sysId>
</e>
</d>
</root>
【问题讨论】:
标签: xml python-2.7 elementtree