• Home
  • Tech
Safha
  • Home
  • Tech
    • All
    • Windows
    What is a Virtual IP (VIP)?

    What is a Virtual IP (VIP)?

    Private Vs Public IP Addresses

    Private Vs Public IP Addresses

    Proxy Vs Reverse Proxy

    Proxy Vs Reverse Proxy

    Content Delivery Network (CDN)

    Content Delivery Network (CDN)

    Building Modern IT Data Centers

    Building Modern IT Data Centers

    What is Virtualisation?

    What is Virtualisation?

No Result
View All Result
  • Home
  • Tech
    • All
    • Windows
    What is a Virtual IP (VIP)?

    What is a Virtual IP (VIP)?

    Private Vs Public IP Addresses

    Private Vs Public IP Addresses

    Proxy Vs Reverse Proxy

    Proxy Vs Reverse Proxy

    Content Delivery Network (CDN)

    Content Delivery Network (CDN)

    Building Modern IT Data Centers

    Building Modern IT Data Centers

    What is Virtualisation?

    What is Virtualisation?

No Result
View All Result
Safha
No Result
View All Result
  • Home
  • Tech
Home Tech

XML file parsing with Python

Chief Editor by Chief Editor
January 29, 2025
in Tech
0
XML file parsing with Python
0
SHARES
11
VIEWS
Share on FacebookShare on Twitter
  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
Previous Post

Installing third party modules in Python

Next Post

How a website is accessed from a Web Server

Chief Editor

Chief Editor

Next Post
How a website is accessed from a Web Server

How a website is accessed from a Web Server

Leave a Reply Cancel reply

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

  • Trending
  • Comments
  • Latest

How to do a clean Install of Windows 11

January 28, 2025
How a website is accessed from a Web Server

How a website is accessed from a Web Server

March 9, 2025
What is Virtualisation?

What is Virtualisation?

February 15, 2025
XML file parsing with Python

XML file parsing with Python

January 29, 2025

How to do a clean Install of Windows 11

0
Unzipping multiple Zip Files in a folder using Python

Unzipping multiple Zip Files in a folder using Python

0
Installing third party modules in Python

Installing third party modules in Python

0
XML file parsing with Python

XML file parsing with Python

0
What is a Virtual IP (VIP)?

What is a Virtual IP (VIP)?

March 7, 2025
Private Vs Public IP Addresses

Private Vs Public IP Addresses

February 21, 2025
Proxy Vs Reverse Proxy

Proxy Vs Reverse Proxy

February 16, 2025
Content Delivery Network (CDN)

Content Delivery Network (CDN)

February 16, 2025
  • Home
  • Tech

© 2025 Safha - We are all about Technology and Finance Learning and Knowledge Sharing

No Result
View All Result
  • Home
  • Tech

© 2025 Safha - We are all about Technology and Finance Learning and Knowledge Sharing