我想从我的xml文件中删除 CommentsCorrections 节点。我使用ElementTree
XML API删除节点。我不知道为什么我的python代码不起作用,以下是python和xml的例子。
Python:
import xml.etree.ElementTree as ET
tree = ET.parse("pubmed_result.xml")
root = tree.getroot()
for CommentsCorrections in root.findall('CommentsCorrections'):
tree.remove(CommentsCorrections)
tree.write("Scheduler.xml")
XML:
<CommentsCorrectionsList>
<CommentsCorrections RefType="Cites">
<RefSource>Clin Infect Dis. 2005 Nov 1;41(9):1254-60</RefSource>
<PMID Version="1">16206099</PMID>
</CommentsCorrections>
<CommentsCorrections RefType="Cites">
<RefSource>J Antimicrob Chemother. 1998 Jun;41 Suppl D:7-11</RefSource>
<PMID Version="1">9688447</PMID>
</CommentsCorrections>
<CommentsCorrections RefType="Cites">
<RefSource>J Antimicrob Chemother. 2008 Mar;61(3):721-8</RefSource>
<PMID Version="1">18218645</PMID>
</CommentsCorrections>
<CommentsCorrections RefType="Cites">
<RefSource>Infection. 1987;15 Suppl 5:S248-53</RefSource>
<PMID Version="1">3325435</PMID>
</CommentsCorrections>
<CommentsCorrections RefType="Cites">
<RefSource>N Engl J Med. 2005 Sep 8;353(10):988-98</RefSource>
<PMID Version="1">16148284</PMID>
</CommentsCorrections>
</CommentsCorrectionsList>
答案 0 :(得分:0)
您需要从root调用remove方法。
for CommentsCorrections in root.findall('CommentsCorrections'):
root.remove(CommentsCorrections)
tree.write("Scheduler.xml")