Python convert dict to xml
In this post, you will learn how to convert dict to XML in the Python programming language.
A Python dictionary (dict) is used to create an arbitrary definition of the character string. The dict() is generally used to create a data dictionary to hold data in key-value pairs. It results in a good mapping between key and value pairs.
In web applications, XML (Extensible Markup Language) is used in many fields. We can easily render data stored in XML. That's why it is the best choice to use in web services and data transport. It stores data in a formatted way that is easy to search and understand. It makes it easier for an informative application to store, retrieve, and display data. It is widely used in business-to-business transactions, generating metadata, e-commerce applications and so on.
Suppose we have the following dictionary that contains student records-
dic = {
"stu1" : {
"name" : "Smith",
"class": "2A",
"year" : 2014
},
"stu2" : {
"name" : "Loriya",
"class": "1A",
"year" : 2016
},
"stu3" : {
"name" : "Snehal",
"class": "3C",
"year" : 2013
}
}
Convert dict to XML using Dicttoxml
To generate XML, first we need to install the dicttoxml module using the following command.
pip3 install dicttoxml
The above command returns a message something like this on successful installation-
Successfully built dicttoxml
Installing collected packages: dicttoxml
Successfully installed dicttoxml-1.7.4
Here is the complete code to convert a dictionary to XML. There are several libraries and methods available to generate XML, but we are using simple Python code to convert data into XML and export it to an XML file.
from dicttoxml import dicttoxml
students = {
"stu1" : {
"name" : "Smith",
"class": "2A",
"year" : 2014
},
"stu2" : {
"name" : "Loriya",
"class": "1A",
"year" : 2016
},
"stu3" : {
"name" : "Snehal",
"class": "3C",
"year" : 2013
}
}
xml=dicttoxml(students)
xmlfile = open("students.xml", "w")
xmlfile.write(xml.decode())
xmlfile.close()
The above code generates 'students.xml' file that contains the following data-
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<stu1 type="dict">
<name type="str">Smith</name>
<class type="str">2A</class>
<year type="int">2014</year>
</stu1>
<stu2 type="dict">
<name type="str">Loriya</name>
<class type="str">1A</class>
<year type="int">2016</year>
</stu2>
<stu3 type="dict">
<name type="str">Snehal</name>
<class type="str">3C</class>
<year type="int">2013</year>
</stu3>
</root>
Related Articles
Read xml file Python
Convert list to dictionary Python
Python dict inside list
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python add list to list
Python convert xml to dict
Eye Detection Program in Python OpenCV
Python program to map two lists into a dictionary
Python OpenCV Histogram of Color Image
Python dict inside list
Python convert dict to xml
Python raise keyword