I have an XML file with the following format:
<dir name="A">
<dir name="B">
<file name="foo.txt"/>
</dir>
<dir name="C">
<dir name="D">
<file name="bar.txt"/>
</dir>
</dir>
</dir>
<dir name="E">
<file name="bat.txt"/>
<file name="cat.txt"/>
</dir>
<dir name="F">
<dir name="G">
<file name="dog.txt"/>
<file name="rabbit.txt"/>
</dir>
</dir>
I would like to use the python ElementTree module in order to remove any elements that contain a element inside them. That is, I would like to get the inner elements of the XML file (the ones that do not contain another element inside them), along with all their children. I want any such element to be set to the outer level. For example, for the above XML file, the corresponding output file would be:
<dir name="B">
<file name="foo.txt"/>
</dir>
<dir name="D">
<file name="bar.txt"/>
</dir>
<dir name="E">
<file name="bat.txt"/>
<file name="cat.txt"/>
</dir>
<dir name="G">
<file name="dog.txt"/>
<file name="rabbit.txt"/>
</dir>
How can I achieve this?