Editing xml file without creating a new one

152 Views Asked by At

I have tried to save the xml file in the following variable and later work on it as normal xml file. This is not working. How can I approach this situation. I need to edit the xml file without editing in the original file and without creating a new xml file. Is that possible?

comment_2 = open("cool.xml").read()

Thanks and Regards

1

There are 1 best solutions below

0
theodore On

You can use xml.etree.ElementTree to parse the XML file and then save it to a variable:

import xml.getElementTree as ET

tree = ET.parse('xml.etr.xml')
root = tree.getroot()
root.save(root)
root_variable = root_variable

Then you can save the xml file to an instance of ElementTree.