Python XML Parsing with xml.etree | Generated by AI

Home 2025.10

Overview of xml.etree in Python

xml.etree is a built-in Python module (specifically xml.etree.ElementTree) for parsing and manipulating XML data. It provides a tree-like structure where you can read, write, modify, and navigate XML elements efficiently. It’s lightweight and doesn’t require external libraries, making it ideal for basic XML tasks. Import it with import xml.etree.ElementTree as ET.

Parsing XML

To load XML into memory, use parsing methods:

import xml.etree.ElementTree as ET

# Parse from string
xml_data = "<root><child>text</child></root>"
root = ET.fromstring(xml_data)

# Parse from file
tree = ET.parse('example.xml')
root = tree.getroot()

Once parsed, you can traverse the XML tree:

# Example XML: <bookstore><book id="1"><title>Python</title></book></bookstore>
for book in root.findall('book'):
    title = book.find('title').text
    book_id = book.attrib['id']
    print(f"Book {book_id}: {title}")

Modifying XML

Edit elements dynamically:

new_child = ET.SubElement(root, 'new_element')
new_child.text = 'Added content'
new_child.set('attr', 'value')

Writing XML

To save modifications back to XML:

xml_output = ET.tostring(root, encoding='unicode')
with open('output.xml', 'w') as f:
    f.write(xml_output)

# Or directly
tree.write('output.xml')

Complete Example

Here’s a full script to parse, modify, and write XML:

import xml.etree.ElementTree as ET

# Input XML string
xml_str = """<library>
  <book id="1"><title>1984</title><author>Orwell</author></book>
</library>"""

root = ET.fromstring(xml_str)

# Add a new book
new_book = ET.SubElement(root, 'book', id="2")
ET.SubElement(new_book, 'title').text = 'Brave New World'
ET.SubElement(new_book, 'author').text = 'Huxley'

# Print all books
for book in root:
    title = book.find('title').text
    author = book.find('author').text
    print(f"{title} by {author}")

# Write to file
ET.tostring(root, encoding='unicode')
# Or root.write('library.xml')

Notes


Back

x-ai/grok-code-fast-1

Donate