How do I remove XML nodes without removing attributes in the parent element which come after?



I'm trying to delete nodes in an xml file. I've managed to get that far but when the script runs it appears to take the attributes belonging to the parent element which come after with it.


Here is the code:



for i, pid in enumerate(root.findall(".//p")):
for cont in pid.findall('membercontribution'):
for col in cont.findall('col'):
cont.remove(col)


tree.write('fofo.xml')


this:



<p id="S6CV0001P0-00507"><member>The Minister for Overseas Development (Mr. Neil Marten)
</member><membercontribution>: a policy
<col>16</col>
foobar barforb </membercontribution></p>


becomes this:



<p id="S6CV0001P0-00507"><member>The Minister for Overseas Development (Mr. Neil Marten)
</member><membercontribution>: a policy </membercontribution></p>


How do I code this so that I get to keep the "foobar barforb" part that comes after?


Thanks in advance!


No comments:

Post a Comment