Register Login

Reading and Writing XML Files in Python

Updated Sep 16, 2021

XML stands for eXtensible Markup Language is a W3C-recommended self-descriptive language. It sounds much like HTML, and it also uses tags, but XML tags are user-made. Developers use this markup language for defining a set of rules in a form that is both human-readable & machine-readable. Developers embed XML to store and transfer data. In this article, you will get to know about read & write an XML file using a Python program.

Element Tree Module

In this article, we will use the ElementTree Python module to perform the read from XML and write to XML file. ElementTree is a notable and latest module that has simple ways to process and utilize XM files. So, to use this, we have to first import this module in our Python code. Just because XML formats are naturally hierarchical data formats, it is a lot simpler to describe them in a tree-like structure. The module provides built-in methods for representing the whole XML document.

Though it is added as a built-in standard Python library, still if you don't find it working, you can install it using the command:

pip install elementtree

Let us now create an XML file

<data>
	<items>
		<item name = "course1"> Python </item>
		<item name = "course2"> Cyber Security </item>
	</items>
</data>

Now read an XML file using Python code:

import xml.etree.ElementTree as elt
tree = elt.parse('dict.xml')
r = tree.getroot()
# one specific item attribute
print('Fetching the attribute for first item:')
print(r[0][0].attrib)
# fetch every item’s attributes
print('\n Fetching all attributes using a loop:')
for elem in r:
    for subel in elem:
        print(subel.attrib)
# one specific item's data
print('\n Fetch data for second item:')
print(r[0][1].text)
print('\n Get all item\'s data frm XML:')
for elem in r:
    for subel in elem:
        print(subel.text)

Output:

Fetching the attribute for first item:
{'name': 'course1'}

 Fetching all attributes using a loop:
{'name': 'course1'}
{'name': 'course2'}

 Fetch data for second item:
 Cyber Security

 Get all item's data frm XML:
 Python
 Cyber Security

Explanation:

Here, we have to first import xml.etree.ElementTree. The parse() function will create a tree-like structure. Now we need to get to the root element. For this we have used tree.getroot() method. Now using that root variable ‘r’, we can fetch the first item of the attribute. The r[][] has two subscript. The first square braces tell us about the child element within the root (here <items>). The second square bracket tells us about which item we want to fetch. This way (r[0][0]), we can specifically fetch an element and the .attrib helps us to fetch its attribute.
Same way, we can use the for loop to traverse each element of the root (r) one by one and displaying the attributes. Again, if we want to fetch a particular text of an item, we can use the subscript technique (r[0][1]) followed by a .text. For reading all the texts within the items, we can use the for loop again.

Finally write the XML file using Python:

from xml.etree import ElementTree
tree = ElementTree.ElementTree()
root = ElementTree.Element("root-tree")
item = ElementTree.Element("item")
item.text = "Rs. 2600"
root.append(item)
tree._setroot(root)
tree.write("dict2.xml")

This will output:

<root-tree> <item>Rs. 2600</item> </root-tree>

Explanation:

First we have to import the ElementTree. Next we have created an object (tree) of ElementTree using ElementTree.ElementTree(). This will automatically allows our XML code to set a closing tag after each element we define. The ElementTree.Element() helps us to create the first item. We will later define it as root item using the _setroot() method. We will again use the Element() to create a child element within it. Now to fit the text inside the child element, we have to use the item.text and set a string value with the assignment operator. Append the child item object with the root object and write the XML file using the write() method and pass a filename as string.

Conclusion:

Reading and writing data to XML becomes an essential utility for software developers because the XML helps in storing and transferring data in an organized format. Also, accessing these items becomes easy.


×