-1

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

Ajith MS
  • 1
  • 3
  • Yes it is possible. XML files are just text so you can open the file, read its contents, modify appropriately then rewrite the file. However, treating it as plain text could lead to problems with XML structure so you should probably look at using any one of several XML handling modules (e.g., *lxml*) – DarkKnight Sep 15 '22 at 07:46

1 Answers1

0

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.

theodore
  • 1
  • 2