How to Parse XML with Python

This post will focus on how to parse XML with Python.

Fortunately, XML is built-in to Python. So, this makes parsing XML fairly straight forward.

Open the file xml1.py. In this program, the XML data presents itself as a string. Note that the syntax for the string are triple quotes. Single quotes are used, because double quotes are part of XML. The new lines are part of the string.

xml1.py

At the beginning of your code, you should put the following import statement to pull in the XML parsing mechanism.

import xml.etree.ElementTree as ET

Below the data string, you see a line of code as follows:

tree = ET.fromstring(data)

The method “fromstring”, in the Element Tree library, passes in the data, and makes it an object. The object is given the name tree. Now, you can look at the underlying data inside the object.

Below is a screenshot that shows the result if you run this program.

result

Next, look at the xml2.py program. This code will parse out the list of users.

Wk5e_Parse_XML_3

In this program, the input gets converted to an object called stuff. A list is then created for each user in users. Notice a path is specified to find all the users. Next, The length of the list is printed, which tells you the number of users.

After you print the number of users, you can loop through your list of users and print the data you want.

Leave a Reply

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