How to Parse XML String in Python Easily
To parse an XML string in Python, use the
xml.etree.ElementTree module's fromstring() function which converts the XML string into an element tree. You can then navigate and extract data from this tree easily.Syntax
The main function to parse an XML string is xml.etree.ElementTree.fromstring(xml_string). It takes a string containing XML data and returns the root element of the parsed XML tree.
You can then use methods like find(), findall(), and text to access elements and their content.
python
import xml.etree.ElementTree as ET root = ET.fromstring(xml_string) # Access elements child = root.find('child_tag') text = child.text if child is not None else None
Example
This example shows how to parse a simple XML string and print the text inside a specific tag.
python
import xml.etree.ElementTree as ET xml_string = ''' <note> <to>Alice</to> <from>Bob</from> <heading>Reminder</heading> <body>Don't forget our meeting tomorrow!</body> </note> ''' root = ET.fromstring(xml_string) to_text = root.find('to').text from_text = root.find('from').text body_text = root.find('body').text print(f"To: {to_text}") print(f"From: {from_text}") print(f"Message: {body_text}")
Output
To: Alice
From: Bob
Message: Don't forget our meeting tomorrow!
Common Pitfalls
- Not importing
xml.etree.ElementTreecorrectly or forgetting to usefromstring()for strings (instead ofparse()which reads files). - Trying to access elements that do not exist without checking for
None, which causes errors. - Confusing XML namespaces which require special handling.
Always check if find() returns None before accessing .text.
python
import xml.etree.ElementTree as ET xml_string = '<root><child>data</child></root>' root = ET.fromstring(xml_string) # Wrong: may cause error if tag not found # print(root.find('missing').text) # Right: check for None child = root.find('missing') if child is not None: print(child.text) else: print('Tag not found')
Output
Tag not found
Quick Reference
Here is a quick summary of useful xml.etree.ElementTree methods for parsing XML strings:
| Method | Description |
|---|---|
| fromstring(xml_string) | Parse XML string and return root element |
| find(tag) | Find first child with given tag |
| findall(tag) | Find all children with given tag |
| text | Get text content of an element |
| attrib | Get dictionary of element attributes |
Key Takeaways
Use xml.etree.ElementTree.fromstring() to parse XML strings in Python.
Always check if find() returns None before accessing element text.
Use find() and findall() to navigate XML elements easily.
Remember fromstring() is for strings; use parse() for XML files.
Handle XML namespaces carefully if your XML uses them.