Skip to content

Instantly share code, notes, and snippets.

@athavan94
Last active February 23, 2023 13:02
Show Gist options
  • Save athavan94/d78a2c59cae90b87b0474460a00ce1ea to your computer and use it in GitHub Desktop.
Save athavan94/d78a2c59cae90b87b0474460a00ce1ea to your computer and use it in GitHub Desktop.
A phyton script that completely removes a special block from an XML file. It removes the block if a tag in that block is empty. In this example, it looks for ContextNumber and checks if it is empty.
import xml.etree.ElementTree as ET
zahl = 0
# XML load
tree = ET.parse('input.xml')
root = tree.getroot()
# Removing the part
for task in root.findall('./Task'):
for transaction in task.findall('./Transaction'):
for issue in transaction.findall('./Issue'):
contextDatas = issue.find('./ContextDatas')
if contextDatas is not None and contextDatas.find('./ContextNumber').text == None:
task.remove(transaction)
zahl += 1
print(str(zahl) + " Transaction Blocks were removed.")
# XML save
tree.write('output.xml', encoding='utf-8', xml_declaration=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment