I'm trying to remove whole element when it contains certain subelement. I Figured out how to and my code works fine on small sample files, but when i try it over big vile (over 20Mb) it removes just some elements. If I run the code repeatedly over results, for some reason it after several runs removes all the elements I expected in first run, which is just random luck I guess, that the code does not give exception or some error in a first place.
XML sample:
<root>
<element1>
<sub1>
<subsub1>
</subsub1>
</sub1>
<sub2>
<subsub2>
<foo STyp="foo">
<bar>
</bar>
</foo>
</subsub2>
</sub2>
</element1>
</root>
I want to delete the whole element1 if it contains <foo STyp="foo"></foo>.
code:
import xml.etree.ElementTree as ET
tree = ET.parse("my.xml")
for root in tree.iter():
for element1 in root:
el = element1.find('foo')
if el is not None:
root.remove(element1)
tree.write('1.xml')
I'm just puzzled it works on smaller file