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 allows to serialize Python dictionary to XML. They are
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
# Converting Python Dictionary to XMLfrom 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.
# Converting Python Dictionary to XML # with a root elemtntfrom 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
# Using dicttoxml for converting Python# Dictionary to XMLfrom dicttoxml import dicttoxml # Data to be parseddata = {'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!
# Pretty printing XML after parsing # it from dictionaryfrom xml.dom.minidom import parseStringfrom dicttoxml import dicttoxml # Data to be parseddata = {'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
# Removing Type Attribute from parsed XMLfrom xml.dom.minidom import parseString # attr_type = False is used # to remove type attributesxml = 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
# Converting Python Dictionary to # XML and saving to a file from dicttoxml import dicttoxmlfrom xml.dom.minidom import parseString # Variable name of Dictionary is dataxml = dicttoxml(data) # Obtain decode string by decode()# functionxml_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.
# Defining custom names for listsfrom dicttoxml import dicttoxml from xml.dom.minidom import parseString # Dictonary to be convertedobj = {'mylist': [u'foo', u'bar', u'baz'], 'mydict': { 'foo': u'bar', 'baz': 1}, 'ok': True} # custom function for defining# item namesmy_item_func = lambda x: 'list_item'xml = dicttoxml(obj, item_func = my_item_func) # Pretty formating XMLxml_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:
# Using parent name in dictionary # as tag name in xml from dicttoxml import dicttoxmlfrom xml.dom.minidom import parseString # Dictionary to be converteddata = { 'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] } # Here x is the parent, you can chose # to do some prcessing or use a part# of the parent name for tag namemy_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>
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


