Fix Python – Python xml ElementTree from a string source?

Question

Asked By – George

The ElementTree.parse reads from a file, how can I use this if I already have the XML data in a string?

Maybe I am missing something here, but there must be a way to use the ElementTree without writing out the string to a file and reading it again.

xml.etree.elementtree

Now we will see solution for issue: Python xml ElementTree from a string source?


Answer

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

This question is answered By – Jim H.

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0