Reading XML files is a common task in Python and can usually be used with built-inModule to complete. This module provides simple and efficient XML parsing and generation functions. Below is a detailed code example and explanation, showing how to use it
To read the XML file.
Code Example
Suppose we have a name calledXML file, the content is as follows:
<data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdpcap>141100</gdpcap> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdpcap>59900</gdpcap> <neighbor name="Malaysia" direction="N"/> </country> <!-- More national data --> </data>
Our goal is to read this file and extract the name, ranking, year and GDP of each country.
import as ET # parse XML filestree = ('') root = () # traverse all child elements under the root element (here is the <country> element)for country in ('country'): # Get the name attribute of the country country_name = ('name') # Get the child elements of ranking, year, and GDP and extract their text content rank = ('rank').text year = ('year').text gdpcap = ('gdpcap').text # Print extracted information print(f"Country: {country_name}") print(f" Rank: {rank}") print(f" Year: {year}") print(f" GDP per capita: {gdpcap}") print() # Iterate through neighbor elements and extract their names and orientation properties for neighbor in ('neighbor'): neighbor_name = ('name') direction = ('direction') print(f" Neighbor: {neighbor_name} (Direction: {direction})") print() # Blank lines separate different countries
Code explanation
-
Import module:
import as ET
We imported
module and rename it to
ET
Easily use. -
Parse XML files:
tree = ('') root = ()
use
()
The function reads the XML file and returns aElementTree
Object. Then, we usegetroot()
Method to get the root element of the XML document. -
Traversing the country elements:
for country in ('country'):
use
findall()
Method to find all the root element<country>
child elements and iterate over them. -
Extract national information:
- use
get()
Method Getting<country>
Elementalname
property. - use
find()
Method search<rank>
、<year>
and<gdpcap>
child elements and use.text
Properties get their text content.
- use
Print country information:
useprint()
Function prints the extracted country information.-
Iterate through neighbor elements:
for neighbor in ('neighbor'):
In each
<country>
Inside the element, usefindall()
Method to find all<neighbor>
child elements and iterate over them. -
Extract neighbor information:
- use
get()
Method Getting<neighbor>
Elementalname
anddirection
property.
- use
Print neighbor information:
useprint()
Function prints the extracted neighbor information.
Through the above steps, we can successfully read the XML file and extract the required information.Modules provide a simple and powerful API that makes processing XML data relatively easy.
This is the end of this article about the implementation method of python reading xml files. For more related python reading xml files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!