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
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
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
from dicttoxml import dicttoxml
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
from xml.dom.minidom import parseString
from dicttoxml import dicttoxml
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
from xml.dom.minidom import parseString
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
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
xml = dicttoxml(data)
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
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
obj = { 'mylist' : [u 'foo' , u 'bar' , u 'baz' ],
'mydict' : {
'foo' : u 'bar' ,
'baz' : 1 },
'ok' : True }
my_item_func = lambda x: 'list_item'
xml = dicttoxml(obj, item_func = my_item_func)
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
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
data = {
'month' :[ 'Jan' , 'Feb' ,
'Mar' , 'Apr' ,
'May' , 'Jun' ,
'Jul' , 'Aug' ,
'Sep' , 'Oct' ,
'Nov' , 'Dec' ]
}
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>