How to Read XML File in Python: Simple Guide
To read an XML file in Python, use the
xml.etree.ElementTree module. Load the file with ElementTree.parse() and access elements using methods like getroot().Syntax
The basic syntax to read an XML file involves importing ElementTree, parsing the file, and getting the root element.
import xml.etree.ElementTree as ET: Imports the XML parsing module.tree = ET.parse('filename.xml'): Loads and parses the XML file.root = tree.getroot(): Gets the root element of the XML tree.
python
import xml.etree.ElementTree as ET tree = ET.parse('file.xml') root = tree.getroot()
Example
This example reads an XML file named sample.xml and prints the tag and text of each child element under the root.
python
import xml.etree.ElementTree as ET # Parse the XML file tree = ET.parse('sample.xml') # Get the root element root = tree.getroot() # Loop through each child of root and print tag and text for child in root: print(f'Tag: {child.tag}, Text: {child.text.strip() if child.text else ""}')
Output
Tag: title, Text: Example XML
Tag: author, Text: John Doe
Tag: year, Text: 2024
Common Pitfalls
Common mistakes when reading XML files include:
- Trying to parse a non-existent or wrong file path causes
FileNotFoundError. - Not handling empty or missing text in elements can cause errors.
- Confusing
parse()(for files) withfromstring()(for XML strings).
Always check the file path and handle missing data carefully.
python
import xml.etree.ElementTree as ET # Wrong way: parsing a non-existent file try: tree = ET.parse('wrongfile.xml') except FileNotFoundError: print('File not found, please check the filename and path.') # Right way: handle missing text safely tree = ET.parse('sample.xml') root = tree.getroot() for child in root: text = child.text.strip() if child.text else 'No text' print(f'Tag: {child.tag}, Text: {text}')
Output
File not found, please check the filename and path.
Tag: title, Text: Example XML
Tag: author, Text: John Doe
Tag: year, Text: 2024
Quick Reference
Summary tips for reading XML files in Python:
- Use
xml.etree.ElementTreefor simple XML parsing. - Use
parse()for files andfromstring()for XML text. - Access elements with
getroot()and iterate children. - Handle missing or empty text to avoid errors.
- Always check file paths before parsing.
Key Takeaways
Use xml.etree.ElementTree.parse() to load XML files safely.
Get the root element with getroot() to start reading XML content.
Handle missing files and empty element text to avoid errors.
Use fromstring() only for XML strings, not files.
Always verify the XML file path before parsing.