I have an issue with ElementTree that I can't quite figure out. I've read all their documentation as well as all the information I could find on this forum. I have a couple elements/nodes that I am trying to remove using ElementTree. I don't get any errors with the following code, but when I look at the output file I wrote the changes to, the elements/nodes that I expected to be removed are still there. I have a document that looks like this:
<data>
<config>
<script filename="test1.txt"></script>
<documentation filename="test2.txt"></script>
</config>
</data>
My code looks as follows:
import xml.etree.ElementTree as ElementTree
xmlTree = ElementTree.parse(os.path.join(sourcePath, "test.xml"))
xmlRoot = xmlTree.getroot()
for doc in xmlRoot.findall('documentation'):
xmlRoot.remove(doc)
xmlTree.write(os.path.join(sourcePath, "testTWO.xml"))
The result is I get the following document:
<data>
<config>
<script filename="test1.txt" />
<documentation filename="test2.txt" />
</config>
</data>
What I need is something more like this. I am not stuck using ElementTree. If there is a better solution with lxml or some other library, I am all ears. I know ElementTree can be a little bit of a pain at times.
<data>
<config>
</config>
</data>