I have a xml file
<root>
<emps>
<employee title="abc">ABC</employee>
<employee title="gef">GEF</employee>
<!-- <employee title="abc">
ABC
</employee>
-->
</emps>
</root>
I want to delete some employee details from this file. Employee details to be deleted are mentioned in other file delemp.txt abc gh
import xml.etree.ElementTree as ET
class CommentedTreeBuilder(ET.XMLTreeBuilder):
def __init__(self, *args, **kwargs):
super(CommentedTreeBuilder, self).__init__(*args, **kwargs)
self._parser.CommentHandler = self.comment
def comment(self, data):
self._target.start(ET.Comment, {})
self._target.data(data)
self._target.end(ET.Comment)
parser=CommentedTreeBuilder()
tree = ET.parse('emps.xml', parser)
#tree = ET.parse('emps.xml')
root = tree.getroot()
for e in root.iter('employee'):
print(e.attrib)
val = 'abc'
#mv1 = root.find("./employee[@title='abc']")
#root.remove(mv1)
#th='//employee[title()="'+val'"]'
#xpath='//employee[title()="'+val+'"]'
#mv2 = root.find(xpath)
#print mv2
#root.remove(mv2)
mmm = root.find("//employee[@title='{}']".format(val))
print mmm
root.remove(mmm)
#"//*[@text='{}']".format(email)
tree.write("emps.xml")
tree = ET.parse('emps.xml')
root = tree.getroot()
for movie in root.iter('employee'):
print(e.attrib)
I am trying to delete xml node. If I give static value as name='abc' in xpath it is working but if I try to give dynamic value it is not working.Please help me