Python convert XML to Dictionary
In this post, you will learn how to convert an XML to a Dictionary using the Python programming language.
In web applications, XML (Extensible Markup Language) is used for many aspects. The data stored in XML can be rendered easily in many applications. That's why it is the best choice for web services and data transport. It stores data in a formatted way that is easy to search and understand. It makes it easier for informative applications to store, retrieve, and display data. It is widely used in e-commerce applications, business-to-business transactions, generating metadata, and so on. But in some cases, there may be a need to convert data from XML to some other format.
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 this article, we will learn to convert data from XML to dictionary using Python. The dictionary structures a good mapping between a key and a value.
Suppose we have the following XML that contains student records-
<?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>
To convert from XML, first we need to install the xmltodict module using the following command.
pip3 install xmltodict
The above command returns a message something like this on successful installation-
Installing collected packages: xmltodict
Successfully installed xmltodict-0.12.0
Here is the complete code to convert an XML to a dictionary using Python. There are several libraries and methods available to convert from XML, but we are using simple Python code to convert data from XML.
import xmltodict
import pprint
my_xml = """<?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>
"""
my_dict = xmltodict.parse(my_xml)
print(my_dict['root']['stu1'])
print(my_dict['root']['stu2']['name'])
print(my_dict['root']['stu3']['name'])
print(my_dict['root']['stu3']['class'])
print(my_dict['root']['stu3']['year'])
Output of the above code-
OrderedDict([('@type', 'dict'), ('name', OrderedDict([('@type', 'str'), ('#text', 'Smith')])), ('class', OrderedDict([('@type', 'str'), ('#text', '2A')])), ('year', OrderedDict([('@type', 'int'), ('#text', '2014')]))])
OrderedDict([('@type', 'str'), ('#text', 'Loriya')])
OrderedDict([('@type', 'str'), ('#text', 'Snehal')])
OrderedDict([('@type', 'str'), ('#text', '3C')])
OrderedDict([('@type', 'int'), ('#text', '2013')])
Related Articles
Find the stop words in nltk Python
Python OpenCV Image Filtering
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
Python dict inside list
Python convert dict to xml
Python raise keyword