Serialize Python dictionary to XML
Last Updated :
28 Apr, 2025
XML is a markup language that is designed to transport data. It was made while keeping it self descriptive in mind. Syntax of XML is similar to HTML other than the fact that the tags in XML aren't pre-defined. This allows for data to be stored between custom tags where the tag contains details about the data and the data is stored in between the opening and closing tags.
Note: You can read more about XML here: XML | Basics and XML | Syntax
Currently, there are two major modules that allow to serialize Python dictionary to XML. They are
- dict2xml
- Dicttoxml
Using dict2xml
For a quick conversion of Python Dictionary to XML, you can use dict2xml. Install it using:
$ pip install dict2xml
Now suppose you have a Dictionary named data in Python which you want to convert to XML
Python3
# Converting Python Dictionary to XML
from dict2xml import dict2xml
data = {'a': 2,
'b': {
'c': 'as',
'f': True},
'd': 7,
}
xml = dict2xml(data)
print(xml)
Output:
<a>2</a>
<b>
<c>as</c>
<f>True</f>
</b>
<d>7</d>
Wrapping around a root element and custom indenting
Suppose you want to wrap the answer around a root element and add indent of 3 spaces.
Python3
# Converting Python Dictionary to XML
# with a root elemtnt
from dict2xml import dict2xml
data = {'a': 2,
'b': {
'c': 'as',
'f': True},
'd': 7,
}
xml = dict2xml(data, wrap ='root', indent =" ")
print(xml)
Output:
<root>
<a>2</a>
<b>
<c>as</c>
<f>True</f>
</b>
<d>7</d>
</root>
Dicttoxml
Dict2xml is good but it doesn't have many options to specify how I want my XML to be formatted or to add attributes in tags. dicttoxml can be used for these purposes. It also works perfectly on Dict-like and Iterable Objects. Install it via terminal or command prompt by typing:
$ pip install dicttoxml
or
$ easy_install dicttoxml
Python3
# Using dicttoxml for converting Python
# Dictionary to XML
from dicttoxml import dicttoxml
# Data to be parsed
data = {'a': 2,
'b': {
'c': 'as',
'f': True},
'd': 7,
}
xml = dicttoxml(data)
print(xml)
Output:
b'<?xml version="1.0" encoding="UTF-8" ?><root><a type="int">2</a><b type="dict"><c type="str">as</c><f type="bool">True</f></b><d type="int">7</d></root>'
Pretty Format the output
Lets pretty format the text so that we can read it clearly. Python already has an inbuilt package for this!
Python3
# Pretty printing XML after parsing
# it from dictionary
from xml.dom.minidom import parseString
from dicttoxml import dicttoxml
# Data to be parsed
data = {'a': 2,
'b': {
'c': 'as',
'f': True},
'd': 7,
}
xml = dicttoxml(data)
dom = parseString(xml)
print(dom.toprettyxml())
Output:
<?xml version="1.0" ?>
<root>
<a type="int">2</a>
<b type="dict">
<c type="str">as</c>
<f type="bool">True</f>
</b>
<d type="int">7</d>
</root>
Removing Type Attributes
You might notice that the tags now contain attributes like type="int"/"dict"/"str" which can be turned off using attr_type=False
Python3
# Removing Type Attribute from parsed XML
from xml.dom.minidom import parseString
# attr_type = False is used
# to remove type attributes
xml = dicttoxml(data, attr_type = False)
print(parseString(xml).toprettyxml())
Output:
<?xml version="1.0" ?>
<root>
<a>2</a>
<b>
<c>as</c>
<f>True</f>
</b>
<d>7</d>
</root>
Saving XML to a file
At times you might need to save XML to a file which can be done as follows
Python3
# Converting Python Dictionary to
# XML and saving to a file
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
# Variable name of Dictionary is data
xml = dicttoxml(data)
# Obtain decode string by decode()
# function
xml_decode = xml.decode()
xmlfile = open("dict.xml", "w")
xmlfile.write(xml_decode)
xmlfile.close()
Output:

Define custom Item names
If you don't want item elements in a list to be called 'item', you can specify the element name using a function that takes the parent element name (i.e. the list name) as an argument.
Python3
# Defining custom names for lists
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
# Dictionary to be converted
obj = {'mylist': [u'foo', u'bar', u'baz'],
'mydict': {
'foo': u'bar',
'baz': 1},
'ok': True}
# custom function for defining
# item names
my_item_func = lambda x: 'list_item'
xml = dicttoxml(obj, item_func = my_item_func)
# Pretty formatting XML
xml_format = parseString(xml).toprettyxml()
print(xml_format)
Output:
<?xml version="1.0" ?>
<root>
<mylist type="list">
<list_item type="str">foo</list_item>
<list_item type="str">bar</list_item>
<list_item type="str">baz</list_item>
</mylist>
<mydict type="dict">
<foo type="str">bar</foo>
<baz type="int">1</baz>
</mydict>
<ok type="bool">True</ok>
</root>
The benefit of taking the parent element name as an argument is that you can write the function to do something with it. Let's say you have an object with some lists of specific items:
Python3
# Using parent name in dictionary
# as tag name in xml
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
# Dictionary to be converted
data = {
'month':['Jan', 'Feb',
'Mar', 'Apr',
'May', 'Jun',
'Jul', 'Aug',
'Sep', 'Oct',
'Nov', 'Dec']
}
# Here x is the parent, you can choose
# to do some processing or use a part
# of the parent name for tag name
my_item_func = lambda x: x+'s'
xml = dicttoxml(data, item_func = my_item_func)
print(parseString(xml).toprettyxml())
Output:
<?xml version="1.0" ?>
<root>
<month type="list">
<months type="str">Jan</months>
<months type="str">Feb</months>
<months type="str">Mar</months>
<months type="str">Apr</months>
<months type="str">May</months>
<months type="str">Jun</months>
<months type="str">Jul</months>
<months type="str">Aug</months>
<months type="str">Sep</months>
<months type="str">Oct</months>
<months type="str">Nov</months>
<months type="str">Dec</months>
</month>
</root>
Similar Reads
Python dictionary values() values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain
2 min read
Turning a Dictionary into XML in Python XML stands for Extensible Markup Language. XML was designed to be self-descriptive and to store and transport data. XML tags are used to identify, store and organize the data. The basic building block of an XML document is defined by tags. An element has a beginning tag and an ending tag. All elemen
3 min read
How to save a Python Dictionary to a CSV File? CSV (Comma-Separated Values) files are a popular format for storing tabular data in a simple, text-based format. They are widely used for data exchange between applications such as Microsoft Excel, Google Sheets and databases. In this article, we will explore different ways to save a Python dictiona
4 min read
Python - XML to JSON A JSON file is a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which is a standard data interchange format. It is primarily used for transmitting data between a web application and a server. A JSON object contains data in the form of a key/value pai
4 min read
Python - JSON to XML A JSON file is a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which is a standard data interchange format. It is primarily used for transmitting data between a web application and a server.A JSON object contains data in the form of a key/value pair
5 min read
Convert Unicode String to Dictionary in Python Python's versatility shines in its ability to handle diverse data types, with Unicode strings playing a crucial role in managing text data spanning multiple languages and scripts. When faced with a Unicode string and the need to organize it for effective data manipulation, the common task is convert
2 min read
pickle â Python object serialization Python is a widely used general-purpose, high-level programming language. In this article, we will learn about pickling and unpickling in Python using the pickle module. The Python Pickle ModuleThe pickle module is used for implementing binary protocols for serializing and de-serializing a Python ob
9 min read
How to read Dictionary from File in Python? In Python, reading a dictionary from a file involves retrieving stored data and converting it back into a dictionary format. Depending on how the dictionary was savedâwhether as text, JSON, or binary-different methods can be used to read and reconstruct the dictionary for further use in your program
3 min read
Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to
4 min read
How to Add New Line in Dictionary in Python Dictionaries are key-value stores that do not inherently support formatting like new lines within their structure. However, when dealing with strings as dictionary values or when outputting the dictionary in a specific way, we can introduce new lines effectively. Let's explore various methods to add
3 min read