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.
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.
import xml.etree.ElementTree as ET
tree = ET.parse('example.xml')
root = tree.getroot()
for child in root:
print(child.tag, child.text)
import xml.etree.ElementTree as ET
root = ET.Element('person')
name = ET.SubElement(root, 'name')
name.text = 'John'
age = ET.SubElement(root, 'age')
age.text = '30'
tree = ET.ElementTree(root)
tree.write('output.xml')
import xml.etree.ElementTree as ET
tree = ET.parse('example.xml')
root = tree.getroot()
# Modify the first 'name' element
root.find('name').text = 'Alice'
tree.write('modified.xml')
import xml.etree.ElementTree as ET
root = ET.Element('person')
name = ET.SubElement(root, 'name', {'lang': 'en'})
name.text = 'John'
tree = ET.ElementTree(root)
tree.write('output_with_attributes.xml')
import xml.etree.ElementTree as ET
tree = ET.parse('example.xml')
root = tree.getroot()
# Remove the 'age' element
age_element = root.find('age')
root.remove(age_element)
tree.write('output_without_age.xml')
import xml.etree.ElementTree as ET
xml_data = '''<people>
<person>
<name>John</name>
<age>30</age>
</person>
<person>
<name>Alice</name>
<age>25</age>
</person>
</people>'''
root = ET.fromstring(xml_data)
# Find all 'person' elements
for person in root.findall('person'):
name = person.find('name').text
age = person.find('age').text
print(f'{name} is {age} years old')
import xml.etree.ElementTree as ET
from xml.dom import minidom
root = ET.Element('person')
name = ET.SubElement(root, 'name')
name.text = 'John'
age = ET.SubElement(root, 'age')
age.text = '30'
# Generate XML string
xml_str = ET.tostring(root, 'utf-8')
parsed_str = minidom.parseString(xml_str)
pretty_str = parsed_str.toprettyxml(indent=" ")
print(pretty_str)
import xml.etree.ElementTree as ET
xml_data = '''<person xmlns="http://example.com">
<name>John</name>
<age>30</age>
</person>'''
namespaces = {'ns': 'http://example.com'}
root = ET.fromstring(xml_data)
# Accessing element with namespace
name = root.find('ns:name', namespaces).text
print(name) # John
import xml.etree.ElementTree as ET
root = ET.Element('person')
name = ET.SubElement(root, 'name')
name.text = 'John'
age = ET.SubElement(root, 'age')
age.text = '30'
# Convert the tree to a string
xml_str = ET.tostring(root, encoding='unicode')
print(xml_str)