Python program to convert XML to Dictionary


The Extensible Markup Language (XML) is a widely used format for representing structured information. It is a markup language that allows us to define our own tags to describe the data and its structure.

XML is designed to be both human-readable and machine-readable. It uses opening and closing tags to enclose elements, which can have attributes and contain nested elements. The structure of XML documents is defined by a set of rules called Document Type Definition (DTD) or XML Schema.

In this article, we will see how to convert XML to a dictionary using Python programming.

Input Output Scenarios

Following is the input-output scenario for the XML to Dictionary conversion −

Input XML

<root>
    <data>
      <fruit>
        <name>Strawberry</name>
        <color>Red</color>
        <price>2.49</price>
      </fruit>
      <vegetable>
        <name>Spinach</name>
        <color>Green</color>
        <price>1.99</price>
      </vegetable>
    </data>
</root>

Output Dictionary

{'root': {'data': {'fruit': {'name': 'Strawberry', 'color': 'Red', 'price': '2.49'}, 'vegetable': {'name': 'Spinach', 'color': 'Green', 'price': '1.99'}}}}

Using the xmltodict library

The xmltodict library in Python provides the xmltodict.parse() function to parse XML data and convert it into a Python dictionary.

To use the xmltodict library for XML to dictionary conversion, we need to install it first. It is not part of the Python standard library, but we can easily install it using pip. Following is the command.x

Example

#importing xmltodict module
import xmltodict

def xml_to_dict(xml_data):
    #converting xml to dictionary
    dict_data = xmltodict.parse(xml_data)
    return dict_data

#defining an xml string
xml_data = '''
<root>
    <data>
      <fruit>
        <name>Strawberry</name>
        <color>Red</color>
        <price>2.49</price>
      </fruit>
      <vegetable>
        <name>Spinach</name>
        <color>Green</color>
        <price>1.99</price>
      </vegetable>
    </data>
</root>
'''

result_dict = xml_to_dict(xml_data)
print('Output Dictionary:')
print(result_dict)

Output

Output Dictionary:
{'root': {'data': {'fruit': {'name': 'Strawberry', 'color': 'Red', 'price': '2.49'}, 'vegetable': {'name': 'Spinach', 'color': 'Green', 'price': '1.99'}}}}

Example

In this example, we can see how to convert XML data from a file into a dictionary using the xmltodict module.

import xmltodict

def xml_file_to_dict(file_path):
    with open(file_path, 'r') as xml_file:
        xml_data = xml_file.read()
        dict_data = xmltodict.parse(xml_data)
        return dict_data

#defining an xml file path
file_path = 'Sample.xml'
result_dict = xml_file_to_dict(file_path)
print('Output Dictionary:')
print(result_dict)

Output

Output Dictionary:
{'market': {'fruits': {'fruit': [{'name': 'Apple', 'color': 'Red', 'price': '1.99'}, {'name': 'Banana', 'color': 'Yellow', 'price': '0.99'}, {'name': 'Orange', 'color': 'Orange', 'price': '1.49'}]}, 'vegetables': {'vegetable': [{'name': 'Carrot', 'color': 'Orange', 'price': '0.79'}, {'name': 'Broccoli', 'color': 'Green', 'price': '1.29'}, {'name': 'Tomato', 'color': 'Red', 'price': '0.99'}]}}}

Using the xml.etree.ElementTree module

The xml.etree.ElementTree.fromstring() function is a method provided by the xml.etree.ElementTree module in the Python Standard Library. It is used to parse XML data in string format and create an Element object that represents the root of the XML tree. Following is the syntax for the fromstring() function. This function returns an Element object representing the root of the XML tree.

xml.etree.ElementTree.fromstring(xml_string)

Where, xml_string is a string containing the XML data to be parsed.

Example

In this example, we will be using the xml.etree.ElementTree.fromstring() function to convert XML to a dictionary.

import xml.etree.ElementTree as ET

def xml_to_dict(xml_data):
    root = ET.fromstring(xml_data)
    result = {}
    for child in root:
        if len(child) == 0:
            result[child.tag] = child.text
        else:
            result[child.tag] = xml_to_dict(ET.tostring(child))
    return result

#defining an xml string
xml_data = '''
<root>
    <data>
      <fruit>
        <name>Strawberry</name>
        <color>Red</color>
        <price>2.49</price>
      </fruit>
      <vegetable>
        <name>Spinach</name>
        <color>Green</color>
        <price>1.99</price>
      </vegetable>
    </data>
</root>
'''

result_dict = xml_to_dict(xml_data)
print('Output Dictionary:')
print(result_dict)

Output

Output Dictionary:
{'data': {'fruit': {'name': 'Strawberry', 'color': 'Red', 'price': '2.49'}, 'vegetable': {'name': 'Spinach', 'color': 'Green', 'price': '1.99'}}}

These are the few examples to convert XML to a dictionary using Python programming.

Updated on: 29-Aug-2023

734 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements