Selected topic
Data Handling
Prefer practical output? Use related tools below while reading.
Python's xml.etree.ElementTree module provides a simple and easy-to-use API for parsing and generating XML files.
### Example: Parsing an XML File
Suppose we have the following XML file, data.xml:
xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>John Smith</author>
<title>XML for Dummies</title>
<genre>Computer</genre>
<price>39.95</price>
<publish_date>2000-10-01</publish_date>
<description>The best book on XML.</description>
</book>
<book id="bk102">
<author>Jane Doe</author>
<title>XSLT for Dummies</title>
<genre>Computer</genre>
<price>29.95</price>
<publish_date>2001-05-01</publish_date>
<description>A great book on XSLT.</description>
</book>
</catalog>python
import xml.etree.ElementTree as ET# Parse the XML file
tree = ET.parse('data.xml')
root = tree.getroot()
# Print out all the book elements
for book in root.findall('book'):
print(f"Book Title: {book.find('title').text}")
print(f"Author: {book.find('author').text}")
print(f"Genre: {book.find('genre').text}")
print("")
# Accessing attributes
print(root.find('book')['id']) # Output: bk101
# Modifying elements
root[0].find('price').text = '49.95'
tree.write('modified_data.xml')
xml.etree.ElementTree module and assign it a shorter alias, ET.ET.parse(), which returns an ElementTree object.tree.getroot().book elements in the tree using findall() and print out their title, author, and genre.[]) on a node (e.g., root.find('book')['id']).root[0].find('price').text = '49.95').xml.etree.ElementTree module provides many more features and methods for parsing, generating, and manipulating XML data.### Advice
ET.parse() method to parse an XML file.tree.root) or square brackets ([]) to access nodes and attributes.findall(), findtext(), and other methods to query the tree for specific elements.