Open In App

Serialize Python dictionary to XML

Improve
Improve
Like Article
Like
Save
Share
Report

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 

  1. dict2xml
  2. 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:
 

python-serialize-dictionary-to-xml

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>

 



Last Updated : 28 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads