How To Read An XML File In Python?

Thousands of terabytes of data is generated every day. This data has been stored in different formats depending upon the application. Using this data, we are building tens of thousands of machine learning and deep learning models every day to solve the real-world problems. XML format is also a data formatting way in which the data is stored. This tutorial is about How to read an XML File in Python. But first lets have a brief introduction about what is XML?

XML stands for Extensible Markup Language. As the name implies, it is a markup language that uses tags to indicate what data is inside a document. The goal of developing XML format was to store and transport data without relying on software and hardware tools. It is both human and machine readable. Reading an XML file is also referred to as parsing the XML file. Parsing means reading the data from the XML file and analyzing it. With Python, you can parse the information and get nice attributes and tags with all elements.

There are four different ways to read data from the XML files which are:

  • Read data from XML file using Minimal Document Object Model (DOM)
  • Parsing XML File using Element Tree Library
  • lxml Parser

Lets discuss these four methods in detail.

If you want to learn more about python programming, visit Python Programming Tutorials.

Read data from XML file using Minimal Document Object Model (DOM)

The mini DOM module of python provides a parse() function which is used to read the XML file. First of all, you need to install and import xml library into your Python environment. Then use the parse() function to parse the data from this file.

from xml.dom import minidom

# parse an xml file
file = minidom.parse('./data.xml')

As shown in the image below, the XML document consists of tags. The code below shows how you can get the name of first tag.

#display the name of first tag or child

Now, we want to know the book Ids. For this, we will use getElementsByTagName() method. It takes the tag name as an input and then by using getAttribute() method we can get the information of that specific attribute. For example We want to know all the book ids. So, in this case, tag is ‘book’ and the attribute is ‘id’.

#use getElementsByTagName() to get tag
models = file.getElementsByTagName('book')

#get ids of all books
for field in models:

Instead of getting the data one by one using tags, You can also print the whole data in the XML file. Pass the parsed file to toprettyxml() method. It prints out the pretty version of data which means that the data is now readable.

prettyxml = file.toprettyxml()
<?xml version="1.0" ?>
	<book id="bk101">
		<author>Gambardella, Matthew</author>
		<title>XML Developer's Guide</title
		<description>An in-depth look at creating applications with XML.</description>
	<book id="bk102">
		<author>Ralls, Kim</author>
		<title>Midnight Rain</title>
                <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
	<book id="bk103">
		<author>Corets, Eva</author>
		<title>Maeve Ascendant</title>
		<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
	<book id="bk104">
		<author>Corets, Eva</author>
		<title>Oberon's Legacy</title>
		<description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description>
	<book id="bk105">
		<author>Corets, Eva</author>
		<title>The Sundered Grail</title>
		<description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to  Oberon's Legacy.</description>
	<book id="bk106">
		<author>Randall, Cynthia</author>   
		<title>Lover Birds</title>
		<description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled. </description>
	<book id="bk107">
		<author>Thurman, Paula</author>
		<title>Splish Splash</title>
		<description>A deep sea diver finds true love twenty thousand leagues beneath the sea </description>
	<book id="bk108">
		<author>Knorr, Stefan</author>
		<title>Creepy Crawlies</title>
		<description>An anthology of horror stories about roaches, centipedes, scorpions  and other insects </description>
	<book id="bk109">
		<author>Kress, Peter</author>
		<title>Paradox Lost</title>
		<genre>Science Fiction</genre>
		<description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description>
	<book id="bk110">
		<author>O'Brien, Tim</author>
		<title>Microsoft .NET: The Programming Bible</title>
		<description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description>
	<book id="bk111">
		<author>O'Brien, Tim</author>
		<title>MSXML3: A Comprehensive Guide</title>
		<description>The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.</description>
	<book id="bk112">
		<author>Galos, Mike</author>
		<title>Visual Studio 7: A Comprehensive Guide</title>
		<description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description>

Parsing XML File using Element Tree Library

To read an XML file in Python, you can use the library module lxml and beautifulsoup bs4.

BeautifulSoup4 supports actual and rite HTML files, and the lxml library supports your reading and writing the XML document. You can install them in your python environment path by executing this command in your OS(operating system) cmd prompt.

Libraries to install before reading an XML file in python

First, print the XML document in python. 

Printing the pretty print XML file in Python is possible using the Element Tree parse function. 

import xml.etree.ElementTree as ET
k = ET.parse(r"D:\DATA_SCIENCE\python\employee.xml")
l = k.getroot()
m =  ET.tostring(l, encoding="unicode")

Using Etree To Read An XML File In Python 

There are many different ways to read an XML file in Python, but the easiest way is to use the xml.etree.ElementTree class.

The execution process goes in the following steps

Step 01

import xml.etree.ElementTree as ET

Step 02

provide a path of  the XML file

k = ET.parse(r"D:\DATA_SCIENCE\python\employee.xml")

step 3

However, to get the top-level element that contains all information about other XML document elements called root, every root has opening and closing tags represented by “<, “And “>” respectively.

# to get the top level element which contains 
#all information about other xml documents elements called root
# every root have opening and closing tags 
#represented by "<", And ">" respectively.
root = tree.getroot()

Step 04

printing the top-level tag/element of an XML document

# printing the top level tag/element of sml document

Step 05

printing first tag attribute from the root

#printing first tag attribute from the root

Step 06

printing the text contained within the first subtags of the 1st tag from the root. 


The source code to read the xml file in python is as follows: 

All element has text and attributes in XML. In our case, the top-level element is <root> here and have child elements. The child element text is “monoj.” A text from the XML document contains the first child element of the top-level element, the root. 

# importing etree as ET
import xml.etree.ElementTree as ET
#provide path of  the xml file
tree = ET.parse(r"D:\DATA_SCIENCE\python\employee.xml")
# to get the top level element which contains 
#all information about other xml documents elements called root
# every root have opening and closing tags 
#represented by "<", And ">" respectively.
root = tree.getroot()
# printing the top level tag/element of sml document
#printing first tag attribute from the root
# printing the text contained within first subtags of the 1th tag from the root

beautifulsoup bs4 to read an xml file in python

BeautifulSoup() will read the document and return a Tree object. A Tree object is a wrapper around a node-set object that contains information about each element/node in the document. The tree object will have a root node or element (which represents the top-level element), and then each element within the document will be defined as its node-set objects inside the tree object.

Step 01

First, import the BeautifulSoup from bs4. 

from bs4 import BeautifulSoup

Then, we’ll create a function called BeautifulSoup(open()) as ‘xml’ that takes in an XML string and returns an object of all the elements found in the file.

Read the XML file to a variable under the name xml_read. However, reading the XML data inside the beautifulsoup parser stores the returned object.

xml_read = BeautifulSoup(open(r"D:\DATA_SCIENCE\python\employee.xml"), 'xml')

Step 2

The print() function will display all instances of tag “row” within the current tag. 

# Finding all instances of tag `row`
tag_row =  xml_read.find_all('row')
print(tag_row )

Step 03

Finding the first instance of a tag and printing its attributes using find()

# Using find() to extract attributes of the first instance of the tag
tag_name = tag_row.find('child', {'name':'monoj'})

The output will be as follows:


On this page, there is a discussion on how to read an XML file in Python using the BeautifulSoup4 method and ElementTree method. Reading an XML file is not challenging, but you need to install the necessary libraries into your Python terminal. If you want to learn more about Python Programming, visit Python Programming Tutorials.

Leave a Comment

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