Python Read XML File Line By Line
In this post, you will learn how to read XML files line by line using the Python programming language. Python provides numerous libraries to parse or split data written in XML.
In web applications, XML (Extensible Markup Language) is used for many aspects. The data stored in XML can be rendered easily in many applications. That's why it is the best choice for web services and data transport. It stores data in a format that is easy to search and understand. It makes it easier for informative applications to store, retrieve, and display data. It is widely used in e-commerce applications, business-to-business transactions, generating metadata, and so on. It allows programmers to develop their own applications to read information from different applications.
Python provides the ElementTree module to parse the XML. Here is a simple application that parses some given XML data and extracts the data elements from the XML. The fromstring method parses the XML from a string constant. For this, we will pass the XML as a string within triple quotes as follows:
import xml.etree.ElementTree as ET
data = '''
<employee>
<name>Alaya</name>
<phone>3290349906</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it.</email>
</employee>'''
tree = ET.fromstring(data)
print('Name:', tree.find('name').text)
print('Email:', tree.find('email').text)
print('Phone:', tree.find('phone').text)
When you run the above code, it returns the following output.
Name: Alaya
Email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Phone: 3290349906
In the second example, we have imported the XML data by reading from an XML file. The parse method parses an XML section into an element tree. Suppose we have the following XML file.
<?xml version="1.0"?>
<company>
<employee>
<name>Alaya</name>
<phone>3290349906</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it.</email>
</employee>
<employee>
<name>Carle</name>
<phone>9059098968</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it.</email>
</employee>
<employee>
<name>Amma</name>
<phone>6750390948</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it.</email>
</employee>
</company>
The given code parses the above XML file line by line-
import xml.etree.ElementTree as ET
tree = ET.parse('employee.xml')
root = tree.getroot()
for elem in root:
for subelem in elem:
print(subelem.text)
The above code returns the following output-
(env) c:\python37\Scripts\projects>parsexml.py
Alaya
3290349906
This email address is being protected from spambots. You need JavaScript enabled to view it.
Carle
9059098968
This email address is being protected from spambots. You need JavaScript enabled to view it.
Amma
6750390948
This email address is being protected from spambots. You need JavaScript enabled to view it.
In the third example, we have counted the employee data using the findall() method and formatted the return data.
import xml.etree.ElementTree as ET
tree = ET.parse('employee.xml')
emp = tree.findall('employee')
print('Employee count:', len(emp))
for ep in emp:
print('Name: ', ep.find('name').text)
print('Email: ', ep.find('email').text)
print('Phone: ', ep.find('phone').text)
The above code returns the following output -
Employee count: 3
Name: Alaya
Email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Phone: 3290349906
Name: Carle
Email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Phone: 9059098968
Name: Amma
Email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Phone: 6750390948
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Python MongoDB example
Write DataFrame to csv Python
Pandas use in Python
Pandas DataFrame example
Python tkinter messagebox options
Python get public IP address
Webbrowser python install
Python GUI examples
PPython turtle graphics code
Factorial program in python using while loop
Python code for snake game
How to read JSON file in Python
Python Xmltodict example
Python dict to XML attributes