Python xml ElementTree from a string source? Python xml ElementTree from a string source? python python

Python xml ElementTree from a string source?


You can parse the text as a string, which creates an Element, and create an ElementTree using that Element.

import xml.etree.ElementTree as ETtree = 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


You need the xml.etree.ElementTree.fromstring(text)

from xml.etree.ElementTree import XML, fromstringmyxml = fromstring(text)