Python allows parsing these XML documents using two modules namely, the xml.etree.ElementTree module and Minidom (Minimal DOM Implementation). Parsing means to read information from a file and split it into pieces by identifying parts of that particular XML file.
xml.etree.ElementTree Module:
This module helps us format XML data in a tree structure which is the most natural representation of hierarchical data.
EXAMPLE:
import xml.etree.ElementTree as ET
mytree = ET.parse('sample.xml')
myroot = mytree.getroot()
print(myroot)
OUTPUT: <Element ‘metadata’ at 0x033589F0>
xml.dom.minidom Module:
This module is basically used by people who are proficient with DOM (Document Object module). DOM applications often start by parsing XML into DOM.
EXAMPLE:
from xml.dom import minidom
dat=minidom.parse('sample.xml')
print(dat)
OUTPUT: <xml.dom.minidom.Document object at 0x03B5A308>
If you can notice the difference between the outputs, mindom creates an DOM object whereas ElementeTree creates elements.