Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles


XML Parsing: Using MINIDOM Vs Element Tree (etree) in Python

By |2020-07-16T06:54:17+00:00November 4th, 2019|

Brief About XML- XML is Extensible Markup Language which stores information in Hierarchical structure. It is similar to HTML but allows you to make your own tags in a totally customized way.
The best part of XML is Self-Descriptive so easy to store data into XML & Awesome in exchanging between different sources.

Now Let’s come to parsing-

Parsing XML means by using a program we generate an internal representation of serialized XML tags. It forms a tree structure internally in memory which is easy to maintain.

minidom vs elementtree

Sample XML Code: sample.xml

<?xml version=”1.0” encoding=” UTF-8”?>
<person id=”001”>
<home resident=”local”>Delhi</home>

Let’s see how MINIDOM parse XML:

It is a library provided by python which is a minimal implementation of a Document object model.
Step1: from xml.dom import minidom //Import minidom library
Step2: root=minidom.parse(‘sample.xml’) //take file as input and parse
Step3: print(root)

Output: < xml.dom.minidom.Document object at 0x03A5B308>
Once you execute this we will be able to devide XML file and retrieve the required data.

Finding Element of Interest using getElementByTagName:
Tag=root.getElementbyTagName(‘home’)[0] // I have put [0] for one output

Output: < DOM Element: item at 0xc4bc00>
To access value of attribute

Output: local
To retrieve data present in these tags

Output: Delhi
Now Let’s come across Element Tree implementation:
(with same xml code)

First using Parse() function:
Step1: import xml.etree.ElementTree as ET
Step2: tree=ET.parse(‘sample.xml’)
Step3: root=tree.getroot()
Step4: print(root)

Output: <Element ‘metadata’ at 0x03A5B308>
Fetching tags using loop
for x in root[0]
print (x.tag, x.attrib)

Output: fname {} lname {} home{‘resident’:’local’} salary {}
If want to print value stored in tags
for x in root[0]

Ram Kumar Delhi 1000
Modifying XML File:
The elements present in xml file can be manipulated by using set() function. Let us see how?
for lname in root.iter(‘lname’):
Output: The last name will be added with nath
Performance comparison b/w minidom and element tree
elementtree vs minidom


  1. Harriett August 28, 2020 at 7:03 pm - Reply

    This is really interesting, You’re a very skilled blogger.

    I’ve joined your feed and look forward to seeking more of your wonderful post. Also, I’ve shared your web site in my social networks!

    • Mirketa Inc September 2, 2020 at 6:01 am - Reply

      We appreciate that, Harriett. Thank You!

Leave A Comment