0
0
PythonHow-ToBeginner · 4 min read

How to Use ElementTree in Python: Syntax and Examples

Use Python's xml.etree.ElementTree module to parse, create, and manipulate XML data easily. Import it with import xml.etree.ElementTree as ET, then use functions like ET.parse() to read XML files or ET.Element() to create XML elements.
📐

Syntax

The xml.etree.ElementTree module provides classes and functions to work with XML data. Key parts include:

  • ET.Element(tag, attrib): Creates a new XML element with a tag and optional attributes.
  • ET.SubElement(parent, tag, attrib): Creates a child element under a parent.
  • ET.parse(filename): Parses an XML file and returns an ElementTree object.
  • tree.getroot(): Gets the root element of the parsed XML tree.
  • element.text: Access or set the text inside an element.
  • ET.tostring(element): Converts an element back to a string.
python
import xml.etree.ElementTree as ET

# Create root element
root = ET.Element('root')

# Add child element
child = ET.SubElement(root, 'child', attrib={'name': 'child1'})
child.text = 'This is child text'

# Convert element to string
xml_str = ET.tostring(root, encoding='unicode')
print(xml_str)
Output
<root><child name="child1">This is child text</child></root>
💻

Example

This example shows how to parse an XML string, access elements, and print their text and attributes.

python
import xml.etree.ElementTree as ET

xml_data = '''
<library>
  <book id="1">
    <title>Python Basics</title>
    <author>John Doe</author>
  </book>
  <book id="2">
    <title>Advanced Python</title>
    <author>Jane Smith</author>
  </book>
</library>
'''

# Parse XML from string
root = ET.fromstring(xml_data)

# Iterate over book elements
for book in root.findall('book'):
    book_id = book.get('id')
    title = book.find('title').text
    author = book.find('author').text
    print(f'Book ID: {book_id}, Title: {title}, Author: {author}')
Output
Book ID: 1, Title: Python Basics, Author: John Doe Book ID: 2, Title: Advanced Python, Author: Jane Smith
⚠️

Common Pitfalls

Common mistakes when using ElementTree include:

  • Trying to parse malformed XML, which causes errors.
  • Using find() or findall() with wrong tag names, returning None or empty lists.
  • Forgetting to encode/decode strings when reading or writing XML files.
  • Modifying elements without saving changes back to a file.

Always check if elements exist before accessing their text or attributes to avoid errors.

python
import xml.etree.ElementTree as ET

xml_data = '<root><item>Value</item></root>'
root = ET.fromstring(xml_data)

# Wrong: accessing a non-existing element
missing = root.find('missing')
# print(missing.text)  # This would raise an AttributeError

# Correct way:
if missing is not None:
    print(missing.text)
else:
    print('Element not found')
Output
Element not found
📊

Quick Reference

Function/MethodDescription
ET.Element(tag, attrib)Create a new XML element with tag and optional attributes
ET.SubElement(parent, tag, attrib)Create a child element under a parent element
ET.parse(filename)Parse an XML file and return an ElementTree object
tree.getroot()Get the root element of the parsed XML tree
element.find(tag)Find the first child element with the given tag
element.findall(tag)Find all child elements with the given tag
element.get(attribute)Get the value of an attribute
element.textGet or set the text content of an element
ET.tostring(element)Convert an element to a string representation

Key Takeaways

Import ElementTree with 'import xml.etree.ElementTree as ET' to work with XML in Python.
Use ET.parse() to read XML files and ET.Element() to create new XML elements.
Always check if elements exist before accessing their text or attributes to avoid errors.
Use ET.tostring() to convert XML elements back to strings for output or saving.
ElementTree is a simple and effective way to parse and create XML data in Python.