11-13-2022, 10:49 AM
Parsing XML Files in Python – 4 Simple Ways
5/5 – (1 vote)
Problem Formulation and Solution Overview
This article will show you various ways to work with an file.
is an acronym for Extensible Markup Language. This file type is similar to HTML. However, https://developer.mozilla.org/en-US/docs...troduction does not have pre-defined tags like HTML. Instead, a coder can define their own tags to meet specific requirements. https://developer.mozilla.org/en-US/docs...troduction is a great way to transmit and share data, either locally or via the internet. This file can be parsed based on standardized https://developer.mozilla.org/en-US/docs...troduction if structured correctly.
To make it more interesting, we have the following running scenario:
Jan, a Bookstore Owner, wants to know the top three (3) selling Books in her store. This data is currently saved in an format.
Question: How would we write code to read in and extract data from an XML file into a Python script?
We can accomplish this by performing the following steps:
Method 1: Use xmltodict()
This method uses the function to read an file, convert it to a and extract the data.
In the current working directory, create an file called . Copy and paste the code snippet below into this file and save it.
In the current working directory, create a Python file called. Copy and paste the code snippet below into this file and save it. This code reads in and parses the above file. If necessary, install the library.
The first line in the above code snippet imports the library. This library is needed to access and parse the file.
The following highlighted section opens in read mode () and saves it as a File Object, fp. If fp was output to the terminal, an object similar to the one below would display.
Next, the function is called and passed one (1) argument, , which reads in and parses the contents of the file. The results save to as a , and the file is closed. The contents of are shown below.
The final highlighted section loops through the above and extracts each book’s and .
Note: The character represents the key on the keyboard.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqX0qqEVpP5s%2Fhqdefault.jpg)
Method 2: Use minidom.parse()
This method uses the function to read and parse an XML file. This example extracts the ID, Title and Sales for each book.
This example differs from Method 1 as this file contains an additional line at the top () of the file and each tag now has an (attribute) assigned to it.
In the current working directory, create an file called . Copy and paste the code snippet below into this file and save it.
In the current working directory, create a Python file called. Copy and paste the code snippet below into this file and save it.
The first line in the above code snippet imports the library. This allows access to various functions to parse the file and retrieve tags and attributes.
The first section of highlighted lines performs the following:
The last section of highlighted lines loop through the books Object and outputs the results to the terminal.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F5MXDZI3jRio%2Fhqdefault.jpg)
Method 3: Use etree
This method uses to read in and parses an XML file. This example extracts the Title and Sales data for each book.
The considers the XML file as a tree structure. Each element represents a node of said tree. Accessing elements is done on an element level.
This example reads in and parses the file created earlier.
The first line in the above code snippet imports the library. This allows access to all nodes of the XML structure.
The following line reads in and parses. The results save as an XML Object to . If output to the terminal, an Object similar to the one below displays.
The following highlighted section uses a loop to iterate through each tag, extracting the , and tags for each book and outputting them to the terminal.
To retrieve the attribute of the tag, run the following code.
This code extracts the attribute from each tag and outputs it to the terminal.
To extract the values, run the following code.
Method 4: Use untangle.parse()
This method uses to parse an XML string.
This example reads in and parses the file shown below. If necessary, install the library.
The library converts an XML file to a Python object. This is a good option when you have a group of items, such as book names.
In the current working directory, create an file called . Copy and paste the code snippet below into this file and save it. If necessary, install the library.
In the current working directory, create a Python file called. Copy and paste the code snippet below into this file and save it.
The first line in the above code snippet imports the library allowing access to the file structure.
The following line reads in and parses the file. The results save to .
The next line calls the function and passes it one (1) argument: List Comprehension. This code iterates through and retrieves the name of each book and saves the results to . If output to the terminal, the following displays:
The next line instantiates a loop, iterates through each book name, and sends it to the terminal.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaBC0VhpXkOQ%2Fhqdefault.jpg)
Summary
This article has shown four (4) ways to work with files to select the best fit for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor – Blockchain
[i]“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd[/i]
5/5 – (1 vote)
Problem Formulation and Solution Overview
This article will show you various ways to work with an
Code:
XML
Code:
XMLCode:
XMLCode:
XMLCode:
XMLTo make it more interesting, we have the following running scenario:
Jan, a Bookstore Owner, wants to know the top three (3) selling Books in her store. This data is currently saved in an
Code:
XML
Question: How would we write code to read in and extract data from an XML file into a Python script?We can accomplish this by performing the following steps:
- Method 1: Use Code:
xmltodict()
- Method 2: Use Code:
minidom.parse()
- Method 3: Use Code:
etree
- Method 4: Use Code:
untangle.parse()
Method 1: Use xmltodict()
This method uses the
Code:
xmltodict()Code:
XMLCode:
DictionaryIn the current working directory, create an
Code:
XMLCode:
books.xmlCode:
<bookstore> <book> <title>Surrender</title> <author>Bono</author> <sales>21987</sales> </book> <book> <title>Going Rogue</title> <author>Janet Evanovich</author> <sales>15986</sales> </book> <book> <title>Triple Cross</title> <author>James Patterson</author> <sales>11311</sales> </book>
</bookstore>In the current working directory, create a Python file called
Code:
books.pyCode:
XMLCode:
xmltodictCode:
import xmltodict with open('books.xml', 'r') as fp: books_dict = xmltodict.parse(fp.read()) fp.close() for i in books_dict: for j in books_dict[i]: for k in books_dict[i][j]: print(f'Title: {k["title"]} \t Sales: {k["sales"]}')The first line in the above code snippet imports the
Code:
xmltodictCode:
XMLThe following highlighted section opens
Code:
books.xmlCode:
rCode:
<_io.TextIOWrapper name='books.xml' mode='r' encoding='cp1252'>Next, the
Code:
xmltodict.parse()Code:
fp.read()Code:
XMLCode:
books_dictCode:
DictionaryCode:
books_dictCode:
{'bookstore': {'book': [{'title': Surrender', 'author': 'Bono', 'sales': '21987'}, {'title': 'Going Rogue', 'author': 'Janet Evanovich', 'sales': '15986'}, {'title': 'Triple Cross', 'author': 'James Patterson', 'sales': '11311'}]}}The final highlighted section loops through the above
Code:
DictionaryCode:
TitleCode:
SalesCode:
Title: Surrender Sales: 21987
Title: Going Rogue Sales: 15986
Title: Triple Cross Sales: 11311
Note: The Code:
\tMethod 2: Use minidom.parse()
This method uses the
Code:
minidom.parse()This example differs from Method 1 as this
Code:
XMLCode:
<?xml version="1.0"?>Code:
<book>Code:
idIn the current working directory, create an
Code:
XMLCode:
books2.xmlCode:
<?xml version="1.0"?>
<bookstore> <storename>Jan's Best Sellers List</storename> <book id="21237"> <title>Surrender</title> <author>Bono</author> <sales>21987</sales> </book> <book id="21946"> <title>Going Rogue</title> <author>Janet Evanovich</author> <sales>15986</sales> </book> <book id="18241"> <title>Triple Cross</title> <author>James Patterson</author> <sales>11311</sales> </book>
</bookstore>In the current working directory, create a Python file called
Code:
books2.pyCode:
from xml.dom import minidom doc = minidom.parse('books2.xml')
name = doc.getElementsByTagName('storename')[0]
books = doc.getElementsByTagName('book') for b in books: bid = b.getAttribute('id') title = b.getElementsByTagName('title')[0] sales = b.getElementsByTagName('sales')[0] print(f'{bid} {title.firstChild.data} {sales.firstChild.data}')The first line in the above code snippet imports the
Code:
minidomCode:
XMLThe first section of highlighted lines performs the following:
- Reads and parse the file and saves the results toCode:
books2.xml. This action creates the Object shown as (1) below.Code:doc
- Retrieves the tag and saves the results toCode:
<storename>. This action creates an Object shown as (2) below.Code:name
- Retrieves the tag for eachCode:
<book>and saves the results toCode:book. This action creates a List of three (3) Objects: one for each book shown as (3) below.Code:books
Code:
(1) <xml.dom.minidom.Document object at 0x0000022D764AFEE0> (2) <DOM Element: storename at 0x22d764f0ee0> (3) [<DOM Element: book at 0x22d764f3a30>, <DOM Element: book at 0x22d764f3c70>, <DOM Element: book at 0x22d764f3eb0>]The last section of highlighted lines loop through the books Object and outputs the results to the terminal.
Code:
21237 Surrender 21987
21946 Going Rogue 15986
18241 Triple Cross 11311Method 3: Use etree
This method uses
Code:
etree
The Code:
etreeThis example reads in and parses the
Code:
books2.xmlCode:
import xml.etree.ElementTree as ET xml_data = ET.parse('books2.xml')
root = xml_data.getroot() for books in root.findall('book'): title = books.find('title').text author = books.find('author').text sales = books.find('sales').text print(title, author, sales)The first line in the above code snippet imports the
Code:
etreeCode:
<tag>The following line reads in and parses
Code:
books2.xmlCode:
xml_dataCode:
<Element 'bookstore' at 0x000001E45E9442C0>The following highlighted section uses a
Code:
forCode:
<book>Code:
<title>Code:
<author>Code:
<sales>Code:
Surrender Bono 21987
Going Rogue Janet Evanovich 15986
Triple Cross James Patterson 11311To retrieve the attribute of the
Code:
<book>This code extracts the
Code:
idCode:
<book>Code:
{'id': '21237'}
{'id': '21946'}
{'id': '18241'}To extract the values, run the following code.
Code:
for id in root.iter('book'): vals = id.attrib.values() for v in vals: print(vals)Code:
21237
21946
18241Method 4: Use untangle.parse()
This method uses
Code:
untangle.parse()This example reads in and parses the
Code:
books3.xmlCode:
untangle
The Code:
untangleIn the current working directory, create an
Code:
XMLCode:
books3.xmlCode:
untangleCode:
<?xml version="1.0"?>
<root> <book name="Surrender"/> <book name="Going Rogue"/> <book name="Triple Cross"/>
</root>In the current working directory, create a Python file called
Code:
books3.pyCode:
import untangle book_obj = untangle.parse('books3.xml')
books = ','.join([book['name'] for book in book_obj.root.book]) for b in books.split(','): print(b)The first line in the above code snippet imports the
Code:
untangleCode:
XMLThe following line reads in and parses the
Code:
books3.xmlCode:
book_objThe next line calls the
Code:
join()Code:
booksCode:
Surrender,Going Rogue,Triple CrossThe next line instantiates a
Code:
forCode:
Surrender
Going Rogue
Triple CrossSummary
This article has shown four (4) ways to work with
Code:
XMLGood Luck & Happy Coding!
Programmer Humor – Blockchain
[i]“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd[/i]

