xmltodict Module in Python: A Practical Reference

Xmltodict Module In Python

In this tutorial, we will see how to install the xmltodict module and use it in our Python programs to work easily with XML files. We will see how to convert XML to Python dictionaries and to JSON format and vice-versa.

Install the xmltodict module using pip

For Python 3 or above we can use the pip3 command to install xmltodict using the terminal.

pip3  install xmltodict

For older versions of Python we can use the following command to install xmltodict.

pip install xmltodict

What is an XML file?

XML stands for extensible markup language and it was designed primarily for storing and transporting data.

It’s a descriptive language that supports writing structured data and we have to use other software to store, send, receive, or display XML data.

The following XML file has data for an airplane such as the year, make, model and color.

<?xml version = "1.0" encoding = "utf-8"?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = "text/xsl"  href = "xslplane.1.xsl" ?>
<plane>
   <year> 1977 </year>
   <make> Cessna </make>
   <model> Skyhawk </model>
   <color> Light blue and white </color>
</plane>

Now, in the following sections, we will play with this airplane data and see how to convert it into Python dictionary and JSON and convert them back to XML format using the xmltodict module.

How to read XML data into a Python dictionary?

We can convert XML files to a Python dictionary using the xmltodict.parse() method in the xmltodict module.

xmltodict.parse() method takes an XML file as input and changes it to Ordered Dictionary.

We can then extract the dictionary data from Ordered Dictionary using dict constructor for Python dictionaries.

#import module
import xmltodict

#open the file
fileptr = open("/home/aditya1117/askpython/plane.xml","r")

#read xml content from the file
xml_content= fileptr.read()
print("XML content is:")
print(xml_content)

#change xml format to ordered dict
my_ordered_dict=xmltodict.parse(xml_content)
print("Ordered Dictionary is:")
print(my_ordered_dict)
print("Year of plane is:")
print(my_ordered_dict['plane']['year'])

#Use contents of ordered dict to make python dictionary
my_plane= dict(my_ordered_dict['plane'])
print("Created dictionary data is:")
print(my_plane)
print("Year of plane is")
print(my_plane['year'])

Output:

XML content is:
<?xml version = "1.0" encoding = "utf-8"?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = "text/xsl"  href = "xslplane.1.xsl" ?>
<plane>
   <year> 1977 </year>
   <make> Cessna </make>
   <model> Skyhawk </model>
   <color> Light blue and white </color>
</plane>

Ordered Dictionary is:
OrderedDict([('plane', OrderedDict([('year', '1977'), ('make', 'Cessna'), ('model', 'Skyhawk'), ('color', 'Light blue and white')]))])
Year of plane is:
1977
Created dictionary data is:
{'year': '1977', 'make': 'Cessna', 'model': 'Skyhawk', 'color': 'Light blue and white'}
Year of plane is
1977

In the above example, we have successfully extracted our airplane data from XML format using xmltodict.parse() method and printed the data both in the form of Ordered Dictionary and dictionary.

How to convert a Python dictionary to XML?

We can convert a python dictionary into the XML format using xmltodict.unparse() method of xmltodict module.

This method accepts the dictionary object as input as returns an XML format data as output.

The only restriction here is that the dictionary should have single root so that XML data can be formatted easily. Otherwise it will cause ValueError .

#import module
import xmltodict

#define dictionary with all the attributes
mydict={'plane':{'year': '1977', 'make': 'Cessna', 'model': 'Skyhawk', 'color':'Light blue and white'}}
print("Original Dictionary of plane data is:")
print(mydict)

#create xml format
xml_format= xmltodict.unparse(my_ordered_dict,pretty=True)
print("XML format data is:")
print(xml_format)

Output:

Original Dictionary of plane data is:
{'plane': {'year': '1977', 'make': 'Cessna', 'model': 'Skyhawk', 'color': 'Light blue and white'}}
XML format data is:
<?xml version="1.0" encoding="utf-8"?>
<plane>
        <year>1977</year>
        <make>Cessna</make>
        <model>Skyhawk</model>
        <color>Light blue and white</color>
</plane>

In the above example, we have created airplane data in XML format from simple python dictionary data. Now we will see how to convert XML data into JSON format.

How to convert XML to JSON?

We can convert XML data into JSON format using the xmltodict module and the json module in python. In this process, we first create an ordered dictionary from XML format using xmltodict.parse() method.

Then we convert the ordered dictionary into JSON format using json.dumps() method which takes ordered dictionary as an argument and converts it into JSON string.

#import module
import xmltodict
import json

#open the file
fileptr = open("/home/aditya1117/askpython/plane.xml","r")

#read xml content from the file
xml_content= fileptr.read()
print("XML content is:")
print(xml_content)

#change xml format to ordered dict
my_ordered_dict=xmltodict.parse(xml_content)
print("Ordered Dictionary is:")
print(my_ordered_dict)
json_data= json.dumps(my_ordered_dict)
print("JSON data is:")
print(json_data)
x= open("plane.json","w")
x.write(json_data)
x.close()

Output:

XML content is:
<?xml version = "1.0" encoding = "utf-8"?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = "text/xsl"  href = "xslplane.1.xsl" ?>
<plane>
   <year> 1977 </year>
   <make> Cessna </make>
   <model> Skyhawk </model>
   <color> Light blue and white </color>
</plane>

Ordered Dictionary is:
OrderedDict([('plane', OrderedDict([('year', '1977'), ('make', 'Cessna'), ('model', 'Skyhawk'), ('color', 'Light blue and white')]))])
JSON data is:
{"plane": {"year": "1977", "make": "Cessna", "model": "Skyhawk", "color": "Light blue and white"}}

In the above example, we have read XML data into xml_content and then xmltodict.parse() creates an ordered dictionary my_ordered_dict and then JSON data is created using json.dumps() method from ordered dictionary.

How to convert JSON data to XML?

Now, let’s convert JSON data into the XML format using the xmltodict module by first converting the JSON data to Python dictionary using json.load() method and then converting the dictionary to XML using xmltodict.unparse().

Again, here restriction is that JSON data should have a single root otherwise it will cause ValueError.

#import module
import xmltodict
import json

#define dictionary with all the attributes
fileptr = open("/home/aditya1117/askpython/plane.json","r")
json_data=json.load(fileptr)
print("JSON data is:")
print(json_data)

#create xml format
xml_format= xmltodict.unparse(json_data,pretty=True)
print("XML format data is:")
print(xml_format)

Output:

JSON data is:
{'plane': {'year': '1977', 'make': 'Cessna', 'model': 'Skyhawk', 'color': 'Light blue and white'}}
XML format data is:
<?xml version="1.0" encoding="utf-8"?>
<plane>
        <year>1977</year>
        <make>Cessna</make>
        <model>Skyhawk</model>
        <color>Light blue and white</color>
</plane>

In the above example, json.load() accepts the file object as argument and parses the data thereby creating a Python dictionary which is stored in json_data . Then we convert the dictionary into XML file using xmltodict.unparse() method.

Conclusion

In this article, we have used the xmltodict module to process XML data. We have seen how to convert XML data to Python dictionary and JSON format and also converted them back into XML format. Happy Learning!