XML file parsing with Python

  1. Copy below text and paste it in Notepad and save it as contacts.xml
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
  <header>
  <FileName>contacts.xml</FileName>
  <FileVersion>1</FileVersion>
  <FileType>xml</FileType>
  <FileDate>2019-12-29</FileDate>
  <FileRecordCount>2</FileRecordCount>
 </header>
 <ContactDetails>
  <FirstName>Aaa</FirstName>
  <LastName>Baa</LastName>
  <Email>xyz@email.com</Email>
  <Phone>0412345678</Phone>
  <Address>
   <StreetNumber>1</StreetNumber>
   <StreetName>Abc st</StreetName>
   <Suburb>Xyz</Suburb>
   <PostCode>1234</PostCode>
   <State>MNO</State>
   <Country>Abcd</Country>
  </Address>
 </ContactDetails>
 <ContactDetails>
<FirstName>Ccc</FirstName>
<LastName>Ddd</LastName>
<Email>lmn@email.com</Email>
<Phone>0418765432</Phone>
<Address>
<StreetNumber>2</StreetNumber>
<StreetName>dcd st</StreetName>
<Suburb>Zyx</Suburb>
<PostCode>4321</PostCode>
<State>OPQ</State>
<Country>Efgh</Country>
</Address>
 </ContactDetails>
</contacts>

2. Open IDLE (python).

3. Open a new file and save it as xmlparsing.py

4. Copy below text and paste it in xmlparsing.py

from xml.dom import minidom
doc = minidom.parse('contacts.xml')
FileName = doc.getElementsByTagName('FileName')[0]
FileVersion = doc.getElementsByTagName('FileVersion')[0]
FileType = doc.getElementsByTagName('FileType')[0]
FileDate = doc.getElementsByTagName('FileDate')[0]
print('FileName = ', FileName.firstChild.data)
print('FileVersion = ', FileVersion.firstChild.data)
print('FileType = ', FileType.firstChild.data)
print('FileDate = ', FileDate.firstChild.data)
loop = doc.getElementsByTagName("ContactDetails")
for i in loop:
    FirstName = i.getElementsByTagName("FirstName")[0]
    LastName = i.getElementsByTagName("LastName")[0]
    Email = i.getElementsByTagName("Email")[0]
    Phone = i.getElementsByTagName("Phone")[0]
    StreetNumber = i.getElementsByTagName("StreetNumber")[0]
    StreetName = i.getElementsByTagName("StreetName")[0]
    Suburb = i.getElementsByTagName("Suburb")[0]
    PostCode = i.getElementsByTagName("PostCode")[0]
    State = i.getElementsByTagName("State")[0]
    Country = i.getElementsByTagName("Country")[0]
    print(FirstName.firstChild.data, LastName.firstChild.data, Email.firstChild.data,
          Phone.firstChild.data, StreetNumber.firstChild.data, StreetName.firstChild.data,
          Suburb.firstChild.data, PostCode.firstChild.data, State.firstChild.data,
          Country.firstChild.data)

5. Save xmlparsing.py and press F5 to execute.

6. Enjoy the output as below.

FileName =  contacts.xml
FileVersion =  1
FileType =  xml
FileDate =  2019-12-29
Aaa Baa xyz@email.com 0412345678 1 Abc st Xyz 1234 MNO Abcd
Ccc Ddd lmn@email.com 0418765432 2 dcd st Zyx 4321 OPQ Efgh