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.

Answer from dgassaway on Stack Overflow
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - Parses an XML section from a string constant. This function can be used to embed “XML literals” in Python code. text is a string containing XML data. parser is an optional parser instance. If not given, the standard XMLParser parser is used.
Discussions

How do i parse a string in python and write it as an xml to a new xml file? - Stack Overflow
I have xml data in string format which is in variable xml_data xml_data=" Tove Jani More on stackoverflow.com
🌐 stackoverflow.com
Best way to pass a "requests" response to ElementTree to parse the XML?
In my admittedly brief experience with XML and requests, you should be doing the correct procedure for parsing an XML response (and FWIW, I haven't yet seen a discernable difference between res.content or res.text) That being said, since you suggest you might not be getting all the data soon enough, you could try incrementally reading in the XML response as suggested here . Additionally, I've seen some suggestions to save the XML response to a temporary file, and parse the XML directly from that file. More on reddit.com
🌐 r/learnpython
8
5
April 7, 2016
Generating XML with Python
I would use xml.etree for this kind of thing. You can generate xml with templates, but there are escaping issues to consider, and you risk generating broken xml. More on reddit.com
🌐 r/learnpython
5
3
March 16, 2016
How to process / parse XML with Python
Best bet: Use lxml . There's a nice tutorial on the site, too . You can also use the built-in XML parser, either with ElementTree (common) or XML DOM (rarer) , or any other related library from the xml module , but most people prefer lxml, as it's much more powerful (and in parts, compatible with ElementTree). More on reddit.com
🌐 r/learnpython
5
11
October 22, 2017
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › web › parsingXML.html
15.2. Parsing XML — Python for Everybody - Interactive
Calling fromstring converts the string representation of the XML into a “tree” of XML elements. When the XML is in a tree, we have a series of methods we can call to extract portions of data from the XML string. The find function searches through the XML tree and retrieves the element that ...
🌐
Real Python
realpython.com › python-xml-parser
A Roadmap to XML Parsers in Python – Real Python
September 25, 2023 - It was an implementation several times faster than the same interface written in C. Today, the regular module uses the fast implementation whenever possible, so you don’t need to bother anymore. You can use the ElementTree API by employing different parsing strategies: The non-incremental strategy loads up the entire document into memory in a DOM-like fashion. There are two appropriately named functions in the module that allow for parsing a file or a Python string with XML content:
🌐
Apify
blog.apify.com › python-parse-xml
How to parse XML in Python
July 16, 2025 - Since we’ve created an XML file with some book data, we’ll use the parse() function to parse the XML data. However, we’ll also show you how to use the fromstring() function to parse XML data stored in a string format.
🌐
lxml
lxml.de › parsing.html
Parsing XML and HTML with lxml
Otherwise, you can resort at runtime to other (usually slower) Python tools for passing decompressed input into lxml or reading from the network. If you want to parse from a string (bytes or text) and still provide a base URL for the document (e.g. to support relative paths in an XInclude), you can pass the base_url keyword argument: >>> root = etree.fromstring(xml, base_url="http://where.it/is/from.xml")
Find elsewhere
🌐
Python Guides
pythonguides.com › convert-string-to-xml-in-python
Convert String to XML in Python: 5 Effective Methods
May 16, 2025 - The lxml library is not part of the standard library, so you need to install it first: ... If your XML data is messy or not well-formed, Python’s BeautifulSoup library is very helpful. It can still read and parse the XML, even if it has errors. from bs4 import BeautifulSoup # Sample XML string (notice it's not perfectly formatted) xml_string = """ <blog> <post category="Python"> <title>Converting Strings to XML</title> <author>John Doe</author> <date>2023-04-15</date> <post category="Data Science"> <title>Machine Learning Basics</title> <author>Jane Smith</author> <date>2023-04-10</date> </p
🌐
Python
wiki.python.org › moin › EscapingXml
Escaping XML
November 15, 2008 - For current information, please visit python.org. If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list. The Python standard library contains a couple of simple functions for escaping strings of text as XML character data.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-parse-xml-in-python-without-using-external-libraries
How to Parse XML in Python Without Using External Libraries
November 12, 2025 - We convert numeric strings to proper types (float for price, int for stock) For nested categories, we first check if the <categories> element exists. Then we iterate through child <category> elements and collect their text · The result is clean Python data structures you can easily work with. You can now use the parser like so: products = parse_product_catalog('products.xml') for product in products: print(f"\nProduct: {product['name']}") print(f" ID: {product['id']}") print(f" Price: {product['currency']} {product['price']}") print(f" Stock: {product['stock']}") print(f" Categories: {', '.join(product['categories'])}")
🌐
Zyte
zyte.com › learn › a-practical-guide-to-xml-parsing-with-python
A Practical Guide to Python XML Parsing
This method is ideal when you need to quickly parse XML content from sources like HTTP responses or dynamically generated strings. When working with web services, you often receive XML data as a response. You can use Python’s requests library to fetch the XML from a URL and then parse it:
🌐
Edureka
edureka.co › blog › python-xml-parser-tutorial
How to Parse and Modify XML in Python?
December 5, 2024 - The first is by using the parse() function and the second is fromstring() function. The parse () function parses XML document which is supplied as a file whereas, fromstring parses XML when supplied as a string i.e within triple quotes.
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › xml
XML parsing — The Hitchhiker's Guide to Python
import untangle obj = untangle.parse('path/to/file.xml') and then you can get the child element’s name attribute like this: obj.root.child['name'] untangle also supports loading XML from a string or a URL. xmltodict is another simple library that aims at making XML feel like working with JSON.
🌐
Oxylabs
oxylabs.io › blog › python-parse-xml
How to Parse XML in Python
The `fromstring` method takes an XML string as an argument and returns the parsed ElementTree object. Next, you can iter over all the child nodes of the root node and print the texts using the below code: ... The Great Gatsby F. Scott Fitzgerald 1925 · So far, you’ve learned how to parse XML files from Python string objects.
🌐
GeeksforGeeks
geeksforgeeks.org › python › xml-parsing-python
XML parsing in Python - GeeksforGeeks
June 28, 2022 - This article focuses on how one can parse a given XML file and extract some useful data out of it in a structured way. XML: XML stands for eXtensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable.That's why, the design goals of XML emphasize simplicity, generality, and usability across the Internet.
🌐
ScrapingDog
scrapingdog.com › blog › xml-parsing-python
How To Parse XML in Python (Detailed Guide)
May 7, 2025 - ElementTree is a class that wraps the element structure and allows the conversion to and from XML. It has the following properties: Each element that is present in the element module will consist of a tag that represents the type of data being stored. The attributes that are stored are Python dictionaries. A text string consisting of the information that needs to be displayed.
🌐
lxml
lxml.de › tutorial.html
The lxml.etree Tutorial
The reasoning behind this difference is that parse() returns a complete document from a file, while the string parsing functions are commonly used to parse XML fragments.
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python - GeeksforGeeks
January 12, 2026 - ElementTree is included in Python’s standard library, so no installation is required. It represents XML data in a tree structure, making it suitable for hierarchical data. ... Parse the XML file.
🌐
Novixys Software
novixys.com › blog › parsing-xml-in-python
Parsing XML in Python | Novixys Software Dev Blog
January 4, 2017 - We use the ElementTree.fromstring() method to parse an XML string.