Skip to main content

Section 15.2 Parsing XML

Look at the code below and predict what will be printed.

Activity 15.2.1.

Here is a simple application that parses some XML and extracts some data elements from the XML:

Activity 15.2.2.

Run this to see what it prints.
The triple single quote ('''), as well as the triple double quote ("""), allow for the creation of strings in Python that span multiple lines.
Calling fromstring converts the string representation of the XML into a β€œtree” of XML elements. When the XML is in a tree, we have a series of methods we can call to extract portions of data from the XML string. The find function searches through the XML tree and retrieves the element that matches the specified tag. The get gets the value of the attribute in that tag.
Using an XML parser such as ElementTree has the advantage that while the XML in this example is quite simple, it turns out there are many rules regarding valid XML, and using ElementTree allows us to extract data from XML without worrying about the rules of XML syntax.

Subsection 15.2.1 Using get to get the value of an attribute

Why do we use get to get the value of an attribute?
Look at the code below and predict what will be printed.

Activity 15.2.3.

Activity 15.2.4.

Run this to see what it prints.

Note 15.2.1.

Just like with dictionaries we can use get to get the value of an attribute and if the attribute isn’t there the default is to return None.

Subsection 15.2.2 Getting Data from the First Element of a Type in XML

You can use find to get the first element of the XML of a specified type. You can the use find on that element to get children tags of that element.

Activity 15.2.5.

Run the code to see what this prints.
What do you think would happen if we looked for the first β€˜author’ in tree rather than in book? Modify the code to see what happens.

Subsection 15.2.3 Fixing Errors in XML

If your XML has errors, what do you think will happen?

Activity 15.2.6.

The following XML has errors. Try to run the code first to see what happens and then fix the XML so that the code runs correctly.

Activity 15.2.7.

The following XML has errors. Try to run the code first to see what happens and then fix the XML so that the code runs correctly.

Subsection 15.2.4 Write Code to Process XML

Activity 15.2.8.

Write code to print the book title, category, author, and year.

Activity 15.2.9.

Write code to print the note’s to, from, body, and time (with region).
You have attempted of activities on this page.