150. XML Parsing with ElementTree

Here are 10 Python code snippets demonstrating XML parsing and creation using Python's xml.etree.ElementTree module. These examples will guide you through different tasks like parsing XML files, creating XML data, and manipulating XML structures.

1. Parsing XML from a String

This example shows how to parse an XML string and extract elements.

import xml.etree.ElementTree as ET

xml_data = '''<person>
    <name>John</name>
    <age>30</age>
    <city>New York</city>
</person>'''

root = ET.fromstring(xml_data)
print(root.tag)  # person
print(root.find('name').text)  # John
print(root.find('age').text)  # 30

Explanation:

  • fromstring() is used to parse an XML string into an Element object.

  • The find() method locates a sub-element by tag.


2. Parsing XML from a File

This snippet demonstrates how to parse XML from a file.

Explanation:

  • parse() reads an XML file and returns an ElementTree object.

  • getroot() fetches the root element, and you can iterate over all child elements.


3. Creating XML Structure

Create an XML document from scratch.

Explanation:

  • Element() creates the root element, and SubElement() adds child elements.

  • write() saves the XML tree to a file.


4. Modifying XML Elements

This example shows how to modify an existing XML element.

Explanation:

  • The find() method locates a specific element, and its text value is updated.


5. Adding Attributes to XML Elements

Demonstrates how to add attributes to XML elements.

Explanation:

  • Attributes are added to elements by passing a dictionary as a second argument to SubElement().


6. Removing an Element from XML

Remove an element from an XML structure.

Explanation:

  • The remove() method is used to delete an element from the XML tree.


7. Searching for Elements with XPath

Using findall() to search for multiple elements that match an XPath-like expression.

Explanation:

  • findall() returns all matching elements.

  • XPath-like expressions can be used to locate nested elements.


8. Pretty-Printing XML

Pretty-print XML data with indentation.

Explanation:

  • minidom.parseString() is used to prettify the XML string.


9. Handling Namespaces in XML

Parsing XML that contains namespaces.

Explanation:

  • A dictionary is passed to find() to handle namespaces.


10. Serializing XML to a String

Serialize an Element to a string format.

Explanation:

  • tostring() serializes the XML tree to a string.

  • The encoding='unicode' argument ensures the output is a string, not bytes.


These examples cover various aspects of XML parsing and manipulation in Python using the xml.etree.ElementTree module, including creating XML from scratch, modifying existing XML, searching for elements, handling namespaces, and more.

Last updated