I have a complex XML for which it's hard to see the structure because each node of level #2 have thousands of children. I'd like to truncate the XML like this:
<main>
<a type="x">
<b>
<b> # should be deleted
<b> # should be deleted
# thousand others
</a>
<a type="y">
<c>
<c> # should be deleted
# many others
</a>
<a type="z">
<d>
<d> # should be deleted
# many others
</a>
</main>
How to keep only one child for each node of level 2, 3, etc. and export the result?
I tried this, but nothing seems deleted:
import xml.etree.ElementTree as ET
tree = ET.parse('in.xml')
root = tree.getroot()
for l1 in root:
print(l1, l1.tag, l1.attrib)
for i, l2 in enumerate(l1):
print(i, l2)
if i > 0:
l1.remove(l2) # nothing seems removed, why?
tree.write('out.xml')