Working with XML in Python involves parsing and generating XML documents. The xml
module in Python’s standard library provides tools for both tasks. Here’s a basic overview of how to work with XML in Python:
- Parsing XML: To parse an XML document, you can use the
xml.etree.ElementTree
module. Here’s an example of parsing an XML document:
import xml.etree.ElementTree as ET
# Parse an XML string
xml_string = '<root><element1>Value 1</element1><element2>Value 2</element2></root>'
root = ET.fromstring(xml_string)
# Access elements and their values
element1 = root.find('element1').text
element2 = root.find('element2').text
print(element1) # Output: Value 1
print(element2) # Output: Value 2
You can also parse an XML file using ET.parse()
.
- Creating XML: To create an XML document, you can use the same
xml.etree.ElementTree
module. Here’s an example of generating XML:
import xml.etree.ElementTree as ET
# Create the root element
root = ET.Element('root')
# Create child elements
element1 = ET.Element('element1')
element1.text = 'Value 1'
element2 = ET.Element('element2')
element2.text = 'Value 2'
# Append child elements to the root
root.append(element1)
root.append(element2)
# Create an ElementTree object
tree = ET.ElementTree(root)
# Write the XML to a file
tree.write('output.xml')
This code creates an XML document with a root element and two child elements, then writes it to a file called “output.xml.”
- Working with Attributes: You can also work with element attributes using the
attrib
dictionary. For example:
element = ET.Element('person', {'name': 'John', 'age': '30'})
print(element.attrib) # Output: {'name': 'John', 'age': '30'}
You can access and modify attributes like a regular dictionary.
- Parsing and Generating XML with Other Libraries: Python has other libraries like
lxml
andxmltodict
that offer additional features and flexibility for working with XML. You may choose one of these libraries depending on your specific needs.
Remember to install any non-standard libraries like lxml
using pip if you plan to use them:
pip install lxml
These are the basics of working with XML in Python using the xml.etree.ElementTree
module. Depending on your project’s complexity, you may explore more advanced features and libraries to suit your needs.