Convert an XML into a dict in Python

In this tutorial, we will learn how to Convert an XML into a dictionary in Python with a simple example.

Nowadays, it is essential for a developer to do some tedious and lengthy programming tasks in an innovative and easiest way to fulfill user requirements as well as to make change easily in the most effective way thus, programmers do not have to change their whole program.

However, in this program, we will implement similar kinds of tricks to convert an XML into a dictionary using Python.

In Python, convert an XML file to a dict

Let’s learn with one instance,

  • Import some libraries
from defusedxml import cElementTree as ElementTree
import warnings

Here, defusedxml is XML bomb protection for Python stdlib modules that defuse XML bombs and other exploits and Warnings will use to ignore deprecation warnings(by using filterwarning function which is implemented below).

warnings.filterwarnings("ignore")
  • Initialize XmltoDict class with it’s functionality
class XmltoDict(dict):
    def __init__(self, parent_element):
        childrenNames = []
        for child in parent_element.getchildren():
            childrenNames.append(child.tag)
        for element in parent_element:
            if element:
                if len(element) == 1 or element[0].tag != element[1].tag:
                    aDict = XmltoDict(element)                    
                if childrenNames.count(element.tag) > 1:
                    try:
                        currentValue = self[element.tag]
                        currentValue.append(aDict)
                        self.update({element.tag: currentValue})
                    except:
                        self.update({element.tag: [aDict]}) 
                else:
                     self.update({element.tag: aDict})
            else:
                self.update({element.tag: element.text})

Now, step by step understand each line which I have put in the above code

  • In first-line I have initialized the class bypassing some passing some values in it, then defined an initial function with self, parent_element parameters.
  • on line 3, I have initialized an empty list
  • then after, for loop is there to fetch child node from the tree and append child.tag values in a list named children names
  • From line 6 to line 20, elements are passed through a loop, where line 7 works like a boolean statement if it is true then pass it to the next condition (Note: We have to use nested if condition) if in line 8 condition match then it will store value in aDict variable similarly, it will pass through further conditions.
  • In the end, self.update will update our data with element tags and dictionary.
  • Lastly,
tree = ElementTree.parse('your Xml file')  
root = tree.getroot() 
xmldict = XmltoDict(root) 
print(xmldict)
  • first, fetch data from XML file then, the Python program will edit the small parts of code and Create an executable program from this modified Python code parsed tree.
  • then,  fetch root elements from data and pass them to the xmldict variable.
  • Finally, print xmldict variable to get output.

 

See below Example (with XML file) with full information beside almost each code to clear your doubts.

xmltodict.py

from defusedxml import cElementTree as ElementTree #defusedxml is XML bomb protection for Python stdlib modules which defuse XML bombs and other exploits.
import warnings # this will use to to ignore deprecation warnings(by using filterwarning function(e.g line 12)).
# Below this will help you to prevent you from an error by simply filtring it with filterwarning function.
warnings.filterwarnings("ignore")

# initiating class
class XmltoDict(dict):

    def __init__(self, parent_element):
        childrenNames = []              # initializing empty list
        for child in parent_element.getchildren(): # .getchildren function will fetch only child node from tree.
            childrenNames.append(child.tag) # .append function will append number of elements into list (e.g childrennames).

        for element in parent_element:
            if element:
                if len(element) == 1 or element[0].tag != element[1].tag: 
                    aDict = XmltoDict(element) # if condition match then store value in aDict variable.

                if childrenNames.count(element.tag) > 1: 
                    try:                                     
                        currentValue = self[element.tag]
                        currentValue.append(aDict)
                        self.update({element.tag: currentValue}) # this will update an element.tag(e.g Book) with it's currunt value.
                    except:                                     # an empty list has to be here inorder to make a list of dictionary.
                        self.update({element.tag: [aDict]}) # it will return list of dictionary with element.tag(e.g Book).

                else:
                     self.update({element.tag: aDict}) # if upper condition gets wrong it will update our value with old one.
            else:
                self.update({element.tag: element.text}) # Here, element.tag are the child elements and element.text are the subchildren.

tree = ElementTree.parse('task1.xml') # fetch data from (e.g task.1.xml) file then, the python program will edit the small parts of code and Create an executable program from this modified python code parsed tree.
root = tree.getroot() # fetch root elements from data (e.g bookstore is root element in our example).
xmldict = XmltoDict(root) # pass root value to XmltoDict function.
print(xmldict) # print Dictionary and it's value in command prompt.

book.xml

<bookstore>  
  <book>    
    <author>Learning XML</author>  
    <year>2012</year>  
    <price>89</price>  
  </book>  
  <book>   
    <author>J K. Rowling</author>  
    <year>2005</year>  
    <price>29</price>  
  </book>
</bookstore>

Your Output:

{'book': [{'author': 'Learning XML', 'year': '2012', 'price': '89'}, {'author': 'J K. Rowling', 'year': '2005', 'price': '29'}]}

Also read: How to serialize Python Dictionary to XML

2 responses to “Convert an XML into a dict in Python”

  1. Bansi says:

    Very useful, thanks for giving this idea

  2. Yashkumar Patel says:

    Thank you, Bansi, good to hear. for more content please go through our website you may find innovative content.

Leave a Reply

Your email address will not be published. Required fields are marked *