我想在xml文件中找到具有特定标记名称的所有节点,让我们说"foo".如果那些foo-tags让它们具有节点名称为"bar"的子节点,那么我想删除这些节点.结果应写入文件.
<myDoc>
<foo>
<bar/> // remove this one
</foo>
<foo>
<anyThing>
<bar/> // don't remove this one
</anyThing>
</foo>
</myDoc>
Run Code Online (Sandbox Code Playgroud)
Thanx任何提示.正如标签所示,我想用python做到这一点.
您可以使用ElementTree:
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse('in.xml')
foos = tree.findall('foo')
for foo in foos:
bars = foo.findall('bar')
for bar in bars:
foo.remove(bar)
tree.write('out.xml')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14548 次 |
| 最近记录: |