目次
1. Introduction
Python is a flexible programming language that can easily handle various data formats. Among them, XML (Extensible Markup Language) is commonly used for exchanging and storing structured data, and is widely employed in API responses and configuration files. In Python, you can easily work with XML using the standard library or third‑party libraries. This article clearly explains everything from basic to advanced XML operations using Python.2. Introduction to Python libraries for handling XML
2.1 Standard Library: xml.etree.ElementTree
Python’s standard library xml.etree.ElementTree is a convenient tool that makes it easy to read and write XML files, as well as explore and edit tree structures.
- Basic parsing:
import xml.etree.ElementTree as ET tree = ET.parse('data.xml') root = tree.getroot() print(root.tag) # Output: "data"
In this code, the XML file named data.xml is read, and the tag of the root element is displayed. You can confirm that the output result is"data"
. - Error handling when the file does not exist:
If the file does not exist, a FileNotFoundError will occur, so you can add exception handling as follows:
try: tree = ET.parse('data.xml') root = tree.getroot() except FileNotFoundError: print("File not found.")
2.2 External Library: lxml
For advanced XML operations, the lxml library is recommended. lxml supports powerful XML processing with XPath and XSLT, making it easy to perform tasks that are difficult to achieve with the standard library.
- Retrieving elements using XPath:
from lxml import etree tree = etree.parse('data.xml') countries = tree.xpath('//country[@name="Liechtenstein"]') print(countries[0].tag) # Output: "country"
In this code, the country element with the attribute name=”Liechtenstein” is retrieved.

3. Parsing and Manipulating XML
3.1 Basic Parsing Steps
To parse an XML file and work with its tree structure, usexml.etree.ElementTree
‘s parse()
. This method reads the XML file and retrieves the tree’s root element.- Parsing from a file:
import xml.etree.ElementTree as ET tree = ET.parse('example.xml') root = tree.getroot() print(root.tag) # Output: "rootTag"
In this code, it reads the XML fileexample.xml
and displays the tag name of the root element.
3.2 Searching Elements and Manipulating Attributes
To search XML elements, usefind()
or findall()
. This allows you to easily retrieve specific elements or attributes.- Searching for elements and retrieving attributes:
country = root.find('country') print(country.get('name')) # Output: "Liechtenstein"
- Manipulating attributes:
To manipulate an XML element’s attributes, retrieve them with
get()
and set them withset()
.country.set('currency', 'CHF') print(country.attrib) # Output: {'name': 'Liechtenstein', 'currency': 'CHF'}
4. Editing and Saving XML
4.1 How to Edit Elements
You can edit the contents of an XML file by modifying the text or attributes of XML elements. For example, to change the text of an element, you can do the following:
- Edit Element Text:
rank = root.find('.rank') rank.text = '2' print(rank.text) # Output: "2"
4.2 Writing to an XML File
To save the modified XML to a file, use the write() method.
- Save XML:
tree.write('updated_data.xml')

5. Advanced Processing with External Libraries
5.1 Advanced XML Manipulation Using lxml
lxml
Using the library, you can perform complex searches with XPath and convert XML to other formats using XSLT.- Searching for Elements with XPath:
from lxml import etree tree = etree.parse('data.xml') countries = tree.xpath('//country') for country in countries: print(country.get('name'))
5.2 Converting XML with XSLT
Here’s an example of converting XML to HTML using XSLT.- XML to HTML Conversion:
from lxml import etree xml_tree = etree.parse('data.xml') xsl_tree = etree.parse('style.xsl') transform = etree.XSLT(xsl_tree) result_tree = transform(xml_tree) result_tree.write('output.html', pretty_print=True)
6. Practice: Sample Code and Explanation
6.1 Example of Extracting Data from XML
This is a sample code that extracts data from an XML file and displays specific information.- Sample Code:
import xml.etree.ElementTree as ET tree = ET.parse('data.xml') root = tree.getroot() for country in root.findall('country'): name = country.get('name') rank = country.find('rank').text print(f"Country: {name}, Rank: {rank}")
6.2 Example of Editing and Saving XML
This is a sample code that edits the values of XML elements and saves the changes.- Sample Code:
import xml.etree.ElementTree as ET tree = ET.parse('data.xml') root = tree.getroot() rank = root.find('.//rank') rank.text = '2' tree.write('updated_data.xml')
7. Summary
When working with XML in Python, using the standard libraryxml.etree.ElementTree
makes basic XML manipulation easy. Additionally, using lxml
enables advanced operations such as XPath and XSLT. Refer to the code presented in the article and try manipulating XML yourself.