Serialize Python Dictionary to XML



The XML is the markup language used to transfer the data. The XML syntax is the same as the HTML, whereas the tags are predefined in HTML and not in XML. This offers us to store the data between the 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:

  • dict2xml (widely used)

  • dicttoxml

Using Dicttoxml

The dicttoxml() is the function used to serialize the dictionary to XML. This function needs to be installed before using it. The following is the code to install the function.

Before using dicttoxml, install it using the following command:

pip install dicttoxml

Syntax

The following is the syntax for using the dicttoxml module. Where, dictionary_name is the name of the dictionary

variable_name = dicttoxml(dictionary_name)

Serializing a Dictionary to XML Using dicttoxml

When we pass the dictionary data to be serialized to the function dicttoxml(), then the data will be transferred to xml format.

from dicttoxml import dicttoxml
d = {"name":["Python","c"],"Age":[20,30]}
print("Created dictionary:",d)
xml = dicttoxml(d)
print("Serialization of dictionary to xml:",xml)

Output

Following is the output for the above code-

Created dictionary: {'name': ['Python', 'c'], 'Age': [20, 30]}
Serialization of dictionary to xml: b'<?xml version="1.0" encoding="UTF-8" ?><root><name type="list"><item type="str">Python</item><item type="str">c</item></name><Age type="list"><item type="int">20</item><item type="int">30</item></Age></root>'

Example

Let's see another example to understand the serialization of a Python dictionary to the XML using dicttoxml.

from dicttoxml import dicttoxml
d = {'a':10,'b':20,'c':30}
print("Created dictionary:",d)
xml = dicttoxml(d)
print("Serialization of dictionary to xml:",xml)

The following is the output of the conversion of the dictionary to xml using the dicttoxml.

Created dictionary: {'a': 10, 'b': 20, 'c': 30}
Serialization of dictionary to xml: b'<?xml version="1.0" encoding="UTF-8" ?><root><a type="int">10</a><b type="int">20</b><c type="int">30</c></root>'

Using Dict2xml

The dict2xml is also a function used to serialize the data of a dictionary to XML. Before calling the function, we have to install. The following is the line of code to install the function.

Before using dict2xml, install it using the following command:

pip install dict2xml

Syntax

The following is the syntax for calling the dict2xml module. Where, dictionary_name is the name of the dictionary.

variable_name = dict2xml(dictionary_name)

Serializing a Dictionary to XML using dict2xml

When we pass the dictionary data to be serialized to the function dict2xml, then the data will be transferred to xml format.

from dict2xml import dict2xml
d = {"name":["Python","c"],"Age":[20,30]}
print("Created dictionary:",d)
xml = dict2xml(d)
print("Serialization of dictionary to xml:",xml)

Output

Following is the output for the above code-

Created dictionary: {'name': ['Python', 'c'], 'Age': [20, 30]}
Serialization of dictionary to xml: <Age>20</Age>
<Age>30</Age>
<name>Python</name>
<name>c</name>

Example

Let's see another example to understand the serialization of a Python dictionary to the XML using the dict2xml function. The following is the code.

from dict2xml import dict2xml
d = {"python":"language","Ball":"item"}
print("Created dictionary:",d)
xml = dict2xml(d)
print("Serialization of dictionary to xml:",xml)

Following is the output for the above code-

Created dictionary: {'python': 'language', 'Ball': 'item'}
Serialization of dictionary to xml: <Ball>item</Ball>
<python>language</python>
Updated on: 2025-02-28T19:25:35+05:30

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements