Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to serialize Python dictionary to XML?
XML (Extensible Markup Language) is a markup language used to transfer and store data. Unlike HTML with predefined tags, XML allows custom tags to organize data between opening and closing tags.
In Python, dictionaries store data as key-value pairs. We can serialize a dictionary to XML format using two popular libraries: dicttoxml and dict2xml.
Using dicttoxml
The dicttoxml library converts Python dictionaries to XML format with type information. Install it using:
pip install dicttoxml
Syntax
from dicttoxml import dicttoxml xml_data = dicttoxml(dictionary_name)
Basic Example
Here's how to serialize a simple dictionary to XML ?
from dicttoxml import dicttoxml
data = {'a': 10, 'b': 20, 'c': 30}
print("Original dictionary:", data)
xml = dicttoxml(data)
print("Serialized XML:", xml.decode('utf-8'))
Original dictionary: {'a': 10, 'b': 20, 'c': 30}
Serialized XML: <?xml version="1.0" encoding="UTF-8" ?><root><a type="int">10</a><b type="int">20</b><c type="int">30</c></root>
Complex Data Types
The dicttoxml library handles lists and nested structures ?
from dicttoxml import dicttoxml
data = {"languages": ["Python", "Java"], "versions": [3.9, 11]}
print("Dictionary with lists:", data)
xml = dicttoxml(data)
print("Serialized XML:", xml.decode('utf-8'))
Dictionary with lists: {'languages': ['Python', 'Java'], 'versions': [3.9, 11]}
Serialized XML: <?xml version="1.0" encoding="UTF-8" ?><root><languages type="list"><item type="str">Python</item><item type="str">Java</item></languages><versions type="list"><item type="float">3.9</item><item type="int">11</item></versions></root>
Using dict2xml
The dict2xml library provides cleaner XML output without type attributes. Install it using:
pip install dict2xml
Syntax
from dict2xml import dict2xml xml_data = dict2xml(dictionary_name)
Basic Example
Here's how dict2xml handles the same dictionary ?
from dict2xml import dict2xml
data = {"python": "language", "ball": "item"}
print("Original dictionary:", data)
xml = dict2xml(data)
print("Serialized XML:")
print(xml)
Original dictionary: {'python': 'language', 'ball': 'item'}
Serialized XML:
<ball>item</ball>
<python>language</python>
Handling Lists
dict2xml creates multiple elements for list values ?
from dict2xml import dict2xml
data = {"name": ["Python", "Java"], "version": [3.9, 11]}
print("Dictionary with lists:", data)
xml = dict2xml(data)
print("Serialized XML:")
print(xml)
Dictionary with lists: {'name': ['Python', 'Java'], 'version': [3.9, 11]}
Serialized XML:
<name>Python</name>
<name>Java</name>
<version>3.9</version>
<version>11</version>
Comparison
| Feature | dicttoxml | dict2xml |
|---|---|---|
| Type Information | Yes (includes type attributes) | No (clean output) |
| Root Element | Automatic <root> | No root wrapper |
| List Handling | <item> elements | Multiple same-name elements |
| XML Declaration | Included | Not included |
Conclusion
Use dicttoxml when you need type information and structured XML with a root element. Use dict2xml for cleaner, simpler XML output without type attributes.
