Python 3.2.5 x64 ElementTree
I have data that I need to format with python. Essentially, I have a file with elements and subelements. I need to remove the children of some of these elements. I checked the previous questions and I could not solve. The best thing is that so far I have only deleted every second child.
Sample data:
<Leg1:MOR oCount="7" xmlns:Leg1="http://what.not"> <Leg1:Order> <Leg1:CTemp id="FO"> <Leg1:Group bNum="001" cCount="4"> <Leg1:Dog ndate="112" pdate="111"/> <Leg1:Dog ndate="122" pdate="121"/> <Leg1:Dog ndate="132" pdate="131"/> <Leg1:Dog ndate="142" pdate="141"/> </Leg1:Group> <Leg1:Group bNum="002" cCount="4"> <Leg1:Dog ndate="112" pdate="111"/> <Leg1:Dog ndate="122" pdate="121"/> <Leg1:Dog ndate="132" pdate="131"/> <Leg1:Dog ndate="142" pdate="141"/> </Leg1:Group> </Leg1:CTemp> <Leg1:CTemp id="GO"> <Leg1:Group bNum="001" cCount="4"> <Leg1:Dog ndate="112" pdate="111"/> <Leg1:Dog ndate="122" pdate="121"/> <Leg1:Dog ndate="132" pdate="131"/> <Leg1:Dog ndate="142" pdate="141"/> </Leg1:Group> <Leg1:Group bNum="002" cCount="4"> <Leg1:Dog ndate="112" pdate="111"/> <Leg1:Dog ndate="122" pdate="121"/> <Leg1:Dog ndate="132" pdate="131"/> <Leg1:Dog ndate="142" pdate="141"/> </Leg1:Group> </Leg1:CTemp> </Leg1:Order> </Leg1:MOR>
What do I need for output:
<Leg1:MOR oCount="7" xmlns:Leg1="http://what.not"> <Leg1:Order> <Leg1:CTemp id="FO"> <Leg1:Group bNum="001" cCount="10"/> <Leg1:Group bNum="002" cCount="10"/> </Leg1:CTemp> <Leg1:CTemp id="GO"> <Leg1:Group bNum="001" cCount="10"/> <Leg1:Group bNum="002" cCount="10"/> </Leg1:CTemp> </Leg1:Order> </Leg1:MOR>
I did not write anything, and my code is useless. I can parse the file and write it. I cannot get the correct processing.
import xml.etree.cElementTree as ET tree = ET.parse("input.xml") root = tree.getroot() for x in root.findall('./Order/CTemp/Group'): root.remove(x) tree.write("output.xml")
How do I remove it from Dog CTemp child elements?
source share