{{announcement.body}}
{{announcement.title}}

Reading an XML File in Python

DZone 's Guide to

Reading an XML File in Python

Easy method to read XML files in Python

· Open Source Zone ·
Free Resource

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:

XML
 




xxxxxxxxxx
1
16


 
1
<database>
2
    <store>
3
        <name>Acme Enterprises</name>
4
        <address>
5
            <number>345</number>
6
            <buildingName>Acme Willow</buildingName>
7
            <streetName>Acme Avenue</streetName
8
            </landmark>
9
            <city>Acme</city>
10
            <zipCode>12345</zipCode>
11
            <type>book,food,stationary,electrical,sanitary,bakery</type>
12
            <timing>9-18</timing>
13
        </address>
14
    </store>
15
</database>
16
 
          



The code to read this input is:

Python
 




x


1
import xmltodict
2
 
          
3
class AddressObject:
4
    def __init__(self, obj):
5
        self.object = obj
6
    def getNumber(self):
7
        return self.object["number"]
8
    def getBuildingName(self):
9
        return self.object["buildingName"]
10
    def getStreetName(self):
11
        return self.object["streetName"]
12
 
          
13
class StoreObject:
14
    def __init__(self, obj):
15
        self.object = obj
16
    def getName(self):
17
        return self.object["name"]
18
    def getAddress(self):
19
        return self.object["address"]
20
    def getAddressObject(self):
21
        addr = self.object["address"]
22
        return AddressObject(addr)
23
 
          
24
if __name__== "__main__":
25
    print("===== xmltodict =====")
26
    with open("store-data.xml") as fd:
27
        doc = xmltodict.parse(fd.read())
28
 
          
29
    database = doc["database"]
30
    
31
    # get the stores in the database
32
    stores = database["store"]
33
    stores2 = doc["database"]["store"]
34
    
35
    for s in stores:
36
        print("name: " + s["name"])
37
        so = StoreObject(s)
38
        print("name from object: " + so.getName())
39
        addr = so.getAddressObject()
40
        print("\taddress - number from object: " + addr.getNumber())
41
        print("\taddress - buliding name from object: " + addr.getBuildingName())
42
        print("\taddress - street name from object: " + addr.getStreetName())
43
 
          
44
    print("===== End =====")
45
 
          



Reading an XML file is highly simplified using the xmltodict package available in Python. Use pip to install and try it.

Topics:
parsing, python, tutorial, xml, xmltodict

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}