Python

Python XML to Dictionary

XML (Extensible Markup Language) is the markup language that is used to store the data. It is a very useful format to store data because it defines a set of rules that allow us to store the data in such a format, which is a machine as well as human-readable. It is widely used to store data. It is an HTML type markup language as they have the same type of structure. However, XML doesn’t offer the defined tags, but you can define your own tags and create your own markup language. The data stored in the XML document can be extracted and analyzed easily. This is the reason that it is most commonly used for web servers. XML is a case sensitive language.

Python comes with many built-in modules and functions to perform specialized tasks. It has many built-in data structures to store and manage the data. Dictionary is one of the very useful built-in data structure that is used to store data in the key-value pair format. We can simply convert the XML data to a Python dictionary. Python provides the xmlodict module to perform the XML related tasks. This article explains the conversion of XML to a dictionary in Python.

Installation of xmltodict module

Before getting started with XML to dictionary conversion, first, we need to install the xmltodict module. The xmltodict can easily be installed using the Python index package (pip). Execute the following command to install the xmltodict module using pip:

pip install xmltodict

If you are using pip3, then execute the following command to install the xmltodict module:

pip3 install xmltodict

It will take a couple of minutes to install the module on your system.

After the successful installation, you will get a message “Successfully installed xmltodict.”

If you are using any Debian based system, then run the following command to install the xmltodict module:

sudo apt install python-xmltodict

The above command is for Python2. In the case of the Python3 version, run the following command:

sudo apt install python3-xmltodict

XML to dictionary conversion

Now let’s convert an XML to Python dictionary. For this purpose, first we need to import the xmltodict module in our Python script. The xmltodict.parse() is a built-in function that convert the XML to Python dictionary.

#importing xmltodict module
import xmltodict
#defining an xml string
xml_data = """
    <student>
      <id>FA18-RSE-012</id>
      <name>
          <firstName>Kamran</firstName>
           <middleName>Kamran</middleName>
            <lastName>Kamran</lastName>
      </name>
      <email>kamran@example.com</email>
      <smeseter>4</smeseter>
      <class>MSSE</class>
      <subjects>
         <sub1>ASPMI</sub1>
         <sub2>ASQA</sub2>
         <sub3>ASPM</sub3>
         <sub4>Semantic Web</sub4>
      </subjects>
    </student>
"""

#converting xml to dictionary
my_dict = xmltodict.parse(xml_data)
#determing the type of converted object
print(type("The type is: ",my_dict))

Output

In the output, it can be seen that the XML is successfully converted into a dictionary.

Now let’s access the data using the dictionary keys. The name of the tags are keys and data inside the tags is actually value.

#importing xmltodict module
import xmltodict
#defining an xml string
xml_data = """
    <student>
      <id>FA18-RSE-012</id>
      <name>
          <firstName>Kamran</firstName>
           <middleName>Sattar</middleName>
            <lastName>Awaisi</lastName>
      </name>
      <email>kamran@example.com</email>
      <semester>4</semester>
      <class>MSSE</class>
      <subjects>
         <sub1>ASPMI</sub1>
         <sub2>ASQA</sub2>
         <sub3>ASPM</sub3>
         <sub4>Semantic Web</sub4>
      </subjects>
    </student>
"""

#converting xml to dictionary
my_dict = xmltodict.parse(xml_data)
#determing the type of converted object
print("The type is: ",type(my_dict))
#accessing the student id
print(my_dict['student']['id'])
#accessing the student name
print(my_dict['student']['name'])
#accessing the student first name
print(my_dict['student']['name']['firstName'])
#accessing the student middle name
print(my_dict['student']['name']['middleName'])
#accessing the student last name
print(my_dict['student']['name']['lastName'])
#accessing the student email
print(my_dict['student']['email'])
#accessing the student semester
print(my_dict['student']['semester'])
#accessing the student class
print(my_dict['student']['class'])
#accessing the student subject3
print(my_dict['student']['subjects']['sub3'])

Output

The output shows that they data is successfully accessed using the keys.

XML to dictionary conversion using files

The XML data is mostly stored in files. The XML files are created with the .xml extension. Now, let’s open the XML file and convert the data into a Python dictionary and access the values using keys.

This is our student.xml file.

Now let’s write our Python script to open the file and convert XML data to Python dictionary.

#importing the module
import xmltodict
#opening the xml file in read mode
with open("student.xml","r") as xml_obj:
    #coverting the xml data to Python dictionary
    my_dict = xmltodict.parse(xml_obj.read())
    #closing the file
    xml_obj.close()
#accessing the name value
print(my_dict['student']['name']['lastName'])
#accessing the email value
print(my_dict['student']['email'])
#accessing the class value
print(my_dict['student']['class'])
#accessing the semester value
print(my_dict['student']['semester'])
#accessing the subject value
print(my_dict['student']['subjects']['sub1'])

Output

Conclusion

XML is a popular format to store data. It is most commonly used to exchange data between web servers and software applications. The XML data can be converted into a Python dictionary using the xmltodict module. This article explains the xmltodict module installation and the XML to dictionary conversion with several examples.

About the author

Kamran Sattar Awaisi

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.