SoFunction
Updated on 2025-03-04

Detailed explanation of how to use the xmltodict library in Python

introduction

In Python programming, processing XML data is a common and important task. XML (Extensible Markup Language) is a markup language used to store and transfer data, and is widely used in fields such as Web services, configuration files and data exchange. However, Python's standard library does not directly provide convenient ways to handle XML, so we need to use third-party libraries to implement this function. This article will introduce the XMLtodict library in detail, a powerful tool that can convert XML data into Python dictionary and vice versa, greatly simplifying the processing of XML data.

Introduction to the xmltodict library

xmltodict is a Python library that provides the functionality to convert XML data into Python dictionary (and convert dictionary back to XML). This library is ideal for handling applications that need to parse or generate XML data, such as web service clients, configuration file readers, and data converters.

Install xmltodict

To use the xmltodict library, you first need to install it into your Python environment. You can use the pip command to do this:

pip install xmltodict

Once the installation is complete, you can import and use the xmltodict library in your Python code.

Basic usage

Convert XML to dictionary

Functions are used to convert XML strings to Python dictionaries.

import xmltodict

xml_data = """
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
"""

# Convert XML to dictionarydata_dict = (xml_data)

print(data_dict)

Output result

{
	'note': {
		'to': 'Tove',
		'from': 'Jani',
		'heading': 'Reminder',
		'body': "Don't forget me this weekend!"
	}
}

The output will be an OrderedDict object that keeps the order of XML elements and converts each element into a key or value of the dictionary.

Convert dictionary to XML

Functions are used to convert Python dictionaries back to XML strings.

import xmltodict

data_dict = {
    'note': {
        'to': 'Tove',
        'from': 'Jani',
        'heading': 'Reminder',
        'body': "Don't forget me this weekend!"
    }
}

# Convert dictionary to XMLxml_data = (data_dict, pretty=True)

print(xml_data)

Output result

<?xml version="1.0" encoding="utf-8"?>
<note>
	<to>Tove</to>
	<from>Jani</from>
	<heading>Reminder</heading>
	<body>Don't forget me this weekend!</body>
</note>

Setting the pretty=True parameter can make the output XML have a good format.

Advanced Usage

Handle complex XML structures

The xmltodict library is capable of handling complex XML containing lists and nested structures.

xml_data = """
<store>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
</store>
"""

data_dict = (xml_data)

print(data_dict)

Output result

{
	'store': {
		'book': [{
			'@category': 'cooking',
			'title': {
				'@lang': 'en',
				'#text': 'Everyday Italian'
			},
			'author': 'Giada De Laurentiis',
			'year': '2005',
			'price': '30.00'
		},
		{
			'@category': 'children',
			'title': {
				'@lang': 'en',
				'#text': 'Harry Potter'
			},
			'author': 'J K. Rowling',
			'year': '2005',
			'price': '29.99'
		}]
	}
}

Processing properties

In XML, elements can have attributes. The xmltodict library parses these properties into keys in the dictionary, preceded by the @ symbol.

xml_data = """
<person >
    <name>John Doe</name>
    <age>30</age>
</person>
"""

data_dict = (xml_data)

print(data_dict)

Output result

{
	'person': {
		'@id': '123',
		'name': 'John Doe',
		'age': '30'
	}
}

The output will contain a person dictionary with the @id attribute.

Error handling

When parsing illegal XML, the xmltodict library throws an exception. You can use the try-except block to catch these exceptions and handle them accordingly.

import xmltodict

xml_data = "&lt;note&gt;&lt;to&gt;Tove&lt;/to&gt;&lt;from&gt;Jani&lt;/note&gt;"  # Missing closed tag
try:
    data_dict = (xml_data)
except Exception as e:
    print(f"Error parsing XML: {e}")

Output result

Error parsing XML: mismatched tag: line 1, column 31

Practical cases

In actual projects, configuration information is usually not written into the code, such as the connection information of the database. These information are stored in the configuration file and the configuration file is read through the code. So let’s try it. When the connection information of the database is actually in the XML configuration file, how to read and use it in the code

Create a configuration()

First create a configuration file and store the connection information of the database into the configuration file

<?xml version="1.0" encoding="UTF-8"?>
<database_config>
    <host>localhost</host>
    <port>3306</port>
    <username>root</username>
    <password>example_password</password>
    <database>test_db</database>
</database_config>

Python code

Import library

import xmltodict  # Import xmltodict library

Define configuration file path

config_file_path = ''

Read the configuration file content

with open(config_file_path, 'r', encoding='utf-8') as file:
    # Read the file content and convert it to a string    config_content = ()

Parse configuration file contents

config_dict = (config_content)  # parse XML content into an ordered dictionary

Extract database configuration information

db_config = config_dict['database_config']

(Optional) Convert an ordered dictionary to a normal dictionary

# db_config = dict(db_config)

Extract specific configuration information

host = db_config['host']  # Database host addressport = int(db_config['port'])  # Database port number, converted to integerusername = db_config['username']  # Database usernamepassword = db_config['password']  # Database Passworddatabase = db_config['database']  # Database name

Print extracted configuration information

print(f"Host: {host}")
print(f"Port: {port}")
print(f"Username: {username}")
print(f"Password: {password}")  # Note: In actual applications, do not print or record passwordsprint(f"Database: {database}")

Connect to the database

Connect to the database using the extracted configuration information (optional part, you need to install the pymysql library)

# import pymysql
# 
# # Create a database connection# connection = (
#     host=host,
#     port=port,
#     user=username,
#     password=password,
#     database=database,
#     charset='utf8mb4',
#     cursorclass=
# )
# 
# try:
#     with () as cursor:
# # Execution query example#         sql = "SELECT VERSION()"
#         (sql)
#         result = ()
#         print(f"Database version: {result['VERSION()']}")
# finally:
#     ()

Complete code

import xmltodict  # Import xmltodict library
# Define the configuration file pathconfig_file_path = ''

# Read the configuration file contentwith open(config_file_path, 'r', encoding='utf-8') as file:
    # Read the file content and convert it to a string    config_content = ()

# Use xmltodict to parse configuration file contentsconfig_dict = (config_content)  # parse XML content into an ordered dictionary
# Extract database configuration information, pay attention to the dictionary structure after parsing xmltodict# config_dict['database_config'] is an ordered dictionary containing all configuration informationdb_config = config_dict['database_config']

# Convert an ordered dictionary to a normal dictionary (if needed)# Note: To simplify the processing, we directly use ordered dictionaries, because ordinary dictionaries do not guarantee order.# If you need to convert to a normal dictionary, you can use the following code:# db_config = dict(db_config)

# Extract specific configuration informationhost = db_config['host']  # Database host addressport = int(db_config['port'])  # Database port number, converted to integerusername = db_config['username']  # Database usernamepassword = db_config['password']  # Database Passworddatabase = db_config['database']  # Database name
# Print extracted configuration informationprint(f"Host: {host}")
print(f"Port: {port}")
print(f"Username: {username}")
print(f"Password: {password}")  # Note: In actual applications, do not print or record passwordsprint(f"Database: {database}")

# Example: Use the extracted configuration information to connect to the database (here, take MySQL as an example, use the pymysql library)# Note: You need to install the pymysql library, you can use pip install pymysql for installation# import pymysql
# 
# # Create a database connection# connection = (
#     host=host,
#     port=port,
#     user=username,
#     password=password,
#     database=database,
#     charset='utf8mb4',
#     cursorclass=
# )
# 
# try:
#     with () as cursor:
# # Execution query example#         sql = "SELECT VERSION()"
#         (sql)
#         result = ()
#         print(f"Database version: {result['VERSION()']}")
# finally:
#     ()

Application scenarios

The xmltodict library is very useful in many application scenarios, including but not limited to:

  1. Web Service Client: parses XML response returned from the Web Service.
  2. Configuration file reader: Read and parse configuration files in XML format.
  3. Data converter: convert XML data to other formats (such as JSON) or perform data processing and analysis, such as converting XML data to JSON format to store it in a database.

Summarize

The xmltodict library is a simple and powerful tool that is capable of converting XML data into a Python dictionary and vice versa. By understanding its basic and advanced usage, you can process XML data more efficiently and integrate it into your Python application. Whether in web service clients, configuration file readers, or data converters, the xmltodict library can provide you with powerful support.

The above is a detailed explanation of the usage method of the xmltodict library in Python. For more information on the usage of the Python xmltodict library, please pay attention to my other related articles!