Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Parsing XML Files in Python – 4 Simple Ways
1
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
Code:
XML
file.

[Image: 2139.png]
Code:
XML
is an acronym for Extensible Markup Language. This file type is similar to HTML. However, https://developer.mozilla.org/en-US/docs...troduction
Code:
XML
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
Code:
XML
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
Code:
XML
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
Code:
XML
format.

[Image: 1f4ac.png] 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
Code:
xmltodict()
function to read an
Code:
XML
file, convert it to a
Code:
Dictionary
and extract the data.

In the current working directory, create an
Code:
XML
file called
Code:
books.xml
. Copy and paste the code snippet below into this file and save it.

Code:
<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.py
. Copy and paste the code snippet below into this file and save it. This code reads in and parses the above
Code:
XML
file. If necessary, install the
Code:
xmltodict
library.

Code:
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:
xmltodict
library. This library is needed to access and parse the
Code:
XML
file.

The following highlighted section opens
Code:
books.xml
in read mode (
Code:
r
) and saves it as a File Object, fp. If fp was output to the terminal, an object similar to the one below would display.

Code:
<_io.TextIOWrapper name='books.xml' mode='r' encoding='cp1252'>

Next, the
Code:
xmltodict.parse()
function is called and passed one (1) argument,
Code:
fp.read()
, which reads in and parses the contents of the
Code:
XML
file. The results save to
Code:
books_dict
as a
Code:
Dictionary
, and the file is closed. The contents of
Code:
books_dict
are shown below.

Code:
{'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:
Dictionary
and extracts each book’s
Code:
Title
and
Code:
Sales
.

Code:
Title: Surrender Sales: 21987 Title: Going Rogue Sales: 15986 Title: Triple Cross Sales: 11311

[Image: 1f4a1.png] Note: The
Code:
\t
character represents the key on the keyboard.

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 2: Use minidom.parse()

This method uses the
Code:
minidom.parse()
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
Code:
XML
file contains an additional line at the top (
Code:
<?xml version="1.0"?>
) of the file and each
Code:
<book>
tag now has an
Code:
id
(attribute) assigned to it.

In the current working directory, create an
Code:
XML
file called
Code:
books2.xml
. Copy and paste the code snippet below into this file and save it.

Code:
<?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.py
. Copy and paste the code snippet below into this file and save it.

Code:
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:
minidom
library. This allows access to various functions to parse the
Code:
XML
file and retrieve tags and attributes.

The first section of highlighted lines performs the following:
  • Reads and parse the
    Code:
    books2.xml
    file and saves the results to
    Code:
    doc
    . This action creates the Object shown as (1) below.

  • Retrieves the
    Code:
    <storename>
    tag and saves the results to
    Code:
    name
    . This action creates an Object shown as (2) below.

  • Retrieves the
    Code:
    <book>
    tag for each
    Code:
    book
    and saves the results to
    Code:
    books
    . This action creates a List of three (3) Objects: one for each book shown as (3) below.

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 11311
[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 3: Use etree

This method uses
Code:
etree
to read in and parses an XML file. This example extracts the Title and Sales data for each book.

[Image: 2139.png] The
Code:
etree
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
Code:
books2.xml
file created earlier.

Code:
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:
etree
library. This allows access to all nodes of the XML
Code:
<tag>
structure.

The following line reads in and parses
Code:
books2.xml
. The results save as an XML Object to
Code:
xml_data
. If output to the terminal, an Object similar to the one below displays.

Code:
<Element 'bookstore' at 0x000001E45E9442C0>

The following highlighted section uses a
Code:
for
loop to iterate through each
Code:
<book>
tag, extracting the
Code:
<title>
,
Code:
<author>
and
Code:
<sales>
tags for each book and outputting them to the terminal.

Code:
Surrender Bono 21987 Going Rogue Janet Evanovich 15986 Triple Cross James Patterson 11311

To retrieve the attribute of the
Code:
<book>
tag, run the following code.

This code extracts the
Code:
id
attribute from each
Code:
<book>
tag and outputs it to the terminal.

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 18241

Method 4: Use untangle.parse()

This method uses
Code:
untangle.parse()
to parse an XML string.

This example reads in and parses the
Code:
books3.xml
file shown below. If necessary, install the
Code:
untangle
library.

[Image: 2139.png] The
Code:
untangle
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
Code:
XML
file called
Code:
books3.xml
. Copy and paste the code snippet below into this file and save it. If necessary, install the
Code:
untangle
library.

Code:
<?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.py
. Copy and paste the code snippet below into this file and save it.

Code:
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:
untangle
library allowing access to the
Code:
XML
file structure.

The following line reads in and parses the
Code:
books3.xml
file. The results save to
Code:
book_obj
.

The next line calls the
Code:
join()
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
Code:
books
. If output to the terminal, the following displays:

Code:
Surrender,Going Rogue,Triple Cross

The next line instantiates a
Code:
for
loop, iterates through each book name, and sends it to the terminal.

Code:
Surrender Going Rogue Triple Cross
[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Summary

This article has shown four (4) ways to work with
Code:
XML
files to select the best fit for your coding requirements.

Good Luck & Happy Coding!

Programmer Humor – Blockchain

[Image: image-31.png][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]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: