Skip to content

Instantly share code, notes, and snippets.

@ameenaziz
Created January 5, 2020 02:50
Show Gist options
  • Save ameenaziz/941d6988da752f6e2b0de17f9326a149 to your computer and use it in GitHub Desktop.
Save ameenaziz/941d6988da752f6e2b0de17f9326a149 to your computer and use it in GitHub Desktop.
Remove xml node and containing parent node that has a specific string
import xml.etree.ElementTree as ET
tree = ET.parse('testdata.xml')
root = tree.getroot()
for parentNode in root.findall('ParentXMLTag'):
childNode = parentNode.find('ChildXMLTag').text
search_string = "Lorem Ipsum"
if childNode == search_string:
root.remove(parentNode)
tree.write('cleaned_data_output.xml')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment