XML is an efficient format for storing and exchanging structured data. As a Python developer, I often need to convert strings containing XML-like data into proper XML objects for parsing, manipulation, and validation.
In this article, I will show you multiple methods to convert a string to XML in Python. Having worked with XML processing as a part of my projects, I have found these approaches to be the most reliable and efficient.
Whether you’re parsing API responses, reading configuration files, or working with legacy systems, these techniques will help you handle XML data with confidence.
What is XML?
XML (eXtensible Markup Language) provides a structured way to represent data using tags. Converting strings to XML allows you to:
- Access and manipulate specific elements using their paths
- Validate the XML against schemas
- Apply XPATH queries for complex data extraction
- Transform the data using XSLT
Let’s get into the methods to perform this conversion in Python.
Read How to Split a String into Characters in Python?
Convert String to XML in Python
Now I will explain how to convert string to XML in Python:
Method 1: Use ElementTree – The Standard Library Approach
Python’s built-in xml.etree.ElementTree library is my go-to solution for most XML processing tasks.
import xml.etree.ElementTree as ET
# Sample XML string
xml_string = """
<employees>
<employee id="1">
<name>John Smith</name>
<position>Software Engineer</position>
<location>New York</location>
</employee>
<employee id="2">
<name>Sarah Johnson</name>
<position>Data Scientist</position>
<location>San Francisco</location>
</employee>
</employees>
"""
# Convert string to XML
root = ET.fromstring(xml_string)
# Accessing data
for employee in root.findall('employee'):
name = employee.find('name').text
position = employee.find('position').text
location = employee.find('location').text
print(f"{name} works as a {position} in {location}")Output:
Chicago: 75°F, Partly Cloudy
Miami: 88°F, SunnyI executed the above example code and added the screenshot below.

This method is simple and part of the standard library, making it my preferred choice for simple XML parsing.
Check out How to Find a Character in a String Using Python?
Method 2: Use minidom – For DOM-Style Access
For those who favor a DOM-based approach, Python’s xml.dom.minidom module offers a convenient way to parse and manipulate XML data.
from xml.dom import minidom
# Sample XML string
xml_string = """
<products>
<product id="101">
<name>MacBook Pro</name>
<price>1999.99</price>
<stock>15</stock>
</product>
<product id="102">
<name>iPhone 13</name>
<price>999.99</price>
<stock>42</stock>
</product>
</products>
"""
# Parse the XML string
dom = minidom.parseString(xml_string)
# Access elements
products = dom.getElementsByTagName('product')
for product in products:
name = product.getElementsByTagName('name')[0].firstChild.data
price = product.getElementsByTagName('price')[0].firstChild.data
print(f"{name}: ${price}")Output:
MacBook Pro: $1999.99
iPhone 13: $999.99I executed the above example code and added the screenshot below.

Minidom provides a more traditional DOM interface that some developers might prefer, especially those coming from other programming languages.
Read How to Convert a String to Lowercase in Python?
Method 3: Use lxml – For Performance and Advanced Features
When working with large XML files or when I need advanced features, I turn to the lxml library. It’s significantly faster than the Python built-in libraries and offers more features.
from lxml import etree
# Sample XML string
xml_string = """
<weather>
<forecast city="Chicago">
<day>Monday</day>
<temperature unit="F">75</temperature>
<condition>Partly Cloudy</condition>
</forecast>
<forecast city="Miami">
<day>Monday</day>
<temperature unit="F">88</temperature>
<condition>Sunny</condition>
</forecast>
</weather>
"""
# Parse the XML string
root = etree.fromstring(xml_string.encode('utf-8'))
# Use XPath for querying (a powerful feature of lxml)
for forecast in root.xpath('//forecast'):
city = forecast.get('city')
temp = forecast.xpath('./temperature/text()')[0]
condition = forecast.xpath('./condition/text()')[0]
print(f"{city}: {temp}°F, {condition}")Output:
Chicago: 75°F, Partly Cloudy
Miami: 88°F, SunnyI executed the above example code and added the screenshot below.

The lxml library is not part of the standard library, so you need to install it first:
pip install lxmlCheck out How to Swap Commas and Dots in a String in Python?
Method 4: Use BeautifulSoup – For Robust Parsing
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>
</post>
</blog>
"""
# Parse the XML string
soup = BeautifulSoup(xml_string, 'xml')
# Access elements
posts = soup.find_all('post')
for post in posts:
if post.title: # BeautifulSoup handles missing tags gracefully
title = post.title.text
category = post.get('category', 'Uncategorized')
print(f"Title: {title}, Category: {category}")For BeautifulSoup with XML parsing, you’ll need to install:
pip install beautifulsoup4 lxmlRead How to Concatenate String and Int in Python?
Method 5: Use dicttoxml – Converting Python Dict to XML
Sometimes I have data in Python dictionaries that I need to convert to XML. The dicttoxml package handles this conversion seamlessly.
import dicttoxml
from xml.dom.minidom import parseString
# Python dictionary representing data
data_dict = {
'states': [
{
'name': 'California',
'capital': 'Sacramento',
'population': 39538223
},
{
'name': 'Texas',
'capital': 'Austin',
'population': 29145505
},
{
'name': 'Florida',
'capital': 'Tallahassee',
'population': 21538187
}
]
}
# Convert dictionary to XML
xml_data = dicttoxml.dicttoxml(data_dict, custom_root='usa', attr_type=False)
# Parse the XML for pretty printing
dom = parseString(xml_data)
pretty_xml = dom.toprettyxml()
print(pretty_xml)
# Convert back to XML object if needed
root = parseString(pretty_xml)To use dicttoxml, install it with:
pip install dicttoxmlRead How to Convert Datetime to String in Python?
Handle Special Characters and Encoding
When working with XML strings, proper handling of special characters and encoding is crucial. XML has specific rules for characters like <, >, &, ', and ".
import xml.etree.ElementTree as ET
import html
# String with special characters
text_with_special_chars = "Product: iPhone & iPad at <Best Buy> for 'limited time'"
# Method 1: Using html module to escape special characters
escaped_text = html.escape(text_with_special_chars)
xml_string = f"<description>{escaped_text}</description>"
# Method 2: Using CDATA for text that shouldn't be parsed
xml_with_cdata = f"""
<description><![CDATA[{text_with_special_chars}]]></description>
"""
# For non-ASCII characters, ensure proper encoding
non_ascii_text = "résumé with café details"
encoded_xml = f"<content>{non_ascii_text}</content>".encode('utf-8')
# Then parse with proper encoding awareness
root = ET.fromstring(encoded_xml.decode('utf-8'))Check out Repeat a String Multiple Times in Python
Validate XML After Conversion
After converting a string to XML, it’s often important to validate it against a schema or DTD.
from lxml import etree
# XML Schema (XSD) as a string
xsd_string = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:positiveInteger"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
"""
# XML to validate
xml_string = """
<person id="p1">
<name>John Smith</name>
<age>35</age>
<email>[email protected]</email>
</person>
"""
# Parse the schema and XML
schema_root = etree.XML(xsd_string.encode('utf-8'))
schema = etree.XMLSchema(schema_root)
xml_doc = etree.fromstring(xml_string.encode('utf-8'))
# Validate
is_valid = schema.validate(xml_doc)
print(f"XML validation result: {is_valid}")
if not is_valid:
print("Validation errors:")
for error in schema.error_log:
print(f" Line {error.line}: {error.message}")Read How to Split String by Whitespace in Python?
Save the XML to a File
After converting your string to XML, you might want to save variables to a file, including the XML data:
import xml.dom.minidom as minidom
# XML string
xml_string = """
<configuration>
<database>
<host>localhost</host>
<port>5432</port>
<user>admin</user>
<password>secure123</password>
</database>
<logging>
<level>INFO</level>
<file>app.log</file>
</logging>
</configuration>
"""
# Parse string and get pretty formatted XML
dom = minidom.parseString(xml_string)
pretty_xml = dom.toprettyxml()
# Save to file
with open('config.xml', 'w', encoding='utf-8') as file:
file.write(pretty_xml)
print("XML has been saved to config.xml")Each of these methods has its strengths, and the best choice depends on your specific requirements. For simple XML processing, ElementTree is sufficient. For more complex scenarios or performance needs, lxml might be a better choice.
Working with XML in Python doesn’t have to be complicated. These methods provide a solid foundation for handling XML data in your applications, from simple parsing to complex transformations.
Remember to handle encoding properly, especially when dealing with international data or special characters. Python’s unicode support, combined with these libraries, makes XML processing simple and reliable.
Related tutorials you may like to read:
- How to Convert an Object to a String in Python?
- How to Remove Characters from a String in Python?
- How to Remove HTML Tags from a String in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.