published by whitemice on Wed, 09/09/2020 - 14:06
Using lxml's etree to iteratively parse an XML document and I wanted to drop a specific element from the stream...
for event, element in etree.iterparse(self.rfile, events=("end",)):
if (event == 'end') and (element.tag == 'row'):
self.wfile.write(etree.tostring(element))
elif (event == 'end') and (element.tag == name_of_element_to_drop):
element.getparent().remove(element) # drop element
The secret sauce is: element.getparent().remove(element)