Reading an XML File in Python
Join the DZone community and get the full member experience.Join For Free
Late last year (2019), after exploring Apache NiFi, I wrote a couple of Python scripts to manipulate NiFi workflows using the NiFi API. One of the scripts read a NiFi template and generated a new template. As it was not a simple 'copy' operation, I had to write a program to read and update various parameters stored in the NiFi template (which is an XML file).
A few weeks ago, I came across a program that had me visting NiFi templates once again. As I was required to do more than simple find and replace, I searched for libraries that would help ease the task of XML manipulation.
On looking around, I came across the xmltodict package and there was no looking back!! I was amazed at the simplicity that xmltodict brings to XML parsing.
The xmltodict package enables us to read and parse an XML file which it then converts into a dictionary. Each node of the XML is represented by a key and value pair in the dictionary. If a node has additional sub-nodes, we can keep adding the name of the node to the dictionary notation and we are able to refer the data. It is that simple.
Let me illustrate with an example. Let us consider the following input:
The code to read this input is:
Reading an XML file is highly simplified using the xmltodict package available in Python. Use pip to install and try it.
Opinions expressed by DZone contributors are their own.