You can parse the text as a string, which creates an Element, and create an ElementTree using that Element.
import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlstring))
I just came across this issue and the documentation, while complete, is not very straightforward on the difference in usage between the parse() and fromstring() methods.
You can parse the text as a string, which creates an Element, and create an ElementTree using that Element.
import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlstring))
I just came across this issue and the documentation, while complete, is not very straightforward on the difference in usage between the parse() and fromstring() methods.
If you're using xml.etree.ElementTree.parse to parse from a file, then you can use xml.etree.ElementTree.fromstring to get the root Element of the document. Often you don't actually need an ElementTree.
See xml.etree.ElementTree
How do i parse a string in python and write it as an xml to a new xml file? - Stack Overflow
Best way to pass a "requests" response to ElementTree to parse the XML?
Generating XML with Python
How to process / parse XML with Python
Videos
With ET.tostring(tree) you get a non-formatted string representation of the XML. To save it to a file:
with open("filename", "w") as f:
f.write(ET.tostring(tree))
In python 3.x
The proposed solution won't work unless you specify with open("filename", "wb") as f: instead of with open("filename", "w") as f: