Python XML Parsing

Python XML Parsing

In this tutorial, we’ll see some examples of using Python to parse XML or Extensible Markup Language. XML is kind of like a more flexible version of HTML. It is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. There are a couple of different ways XML is parsed by computers. The first is known as the Simple API for XML, also known as SAX. The other way to parse XML is by using the DOM or Document Object Model. Back to SAX for a moment. SAX reads XML data one character at a time all the way to the end of the document. As the XML is read, the parser emits events that relate to the XML content. Using Python, we can handle these events as they happen.


SAX Events

When the parser encounters XML as we see below, it generates an event for when it is starting, and then as the parser reaches this closing angle bracket of the opening tag, it will send a start tag event with the tag’s name, and a collection of the attributes, and their values. When the parser reaches the opening angle bracket of the closing tag it will send an end tag event and when it reaches the closing bracket of the closing tag it will send an event for that as well.

sax XML API parse

As these events are generated, we can use Python to respond and operate on the data. When using SAX, the content of the XML can not be accessed in random order. Remember, SAX works by moving through the XML file character by character until it reaches the end of the document. You can not “rewind” or back up during this process. Additionally, SAX can not change the XML data during processing. For this reason, SAX is good when using XML as a config file.


SAX API

To use the SAX API in Python, we use the xml.sax module. So we will be importing that module to run some test code. Once imported, we’ll have access to an xml.sax.parse() function that can work with a file or a stream object. Another function we can use is the xml.sax.parseString() function that can be used if you already have the XML in a string variable. In addition to these functions is a base class named ContentHandler which can be used for custom content processing. The ContentHandler class has functions for handling the start and the end of the document, the start and end of tags, and handling text data. You can create your own class that overrides these functions to handle each type of content.

Python SAX XML Example

Below we have some sample XML data. It is stored in a file names xmldata.xml.

<?xml version="1.0" encoding="UTF-8"?>
<blogposts title="Blog Posts Collection" date="A date" author="Some dude">

   <post type="summary">
      <title>Parse XML With SAX</title>
   </post>

   <post type="detail">
      <title>Overview</title>
      <entry>
         Parsing XML is great
      </entry>
      <entry />
      <entry>
         Have fun with XML parsing
      </entry>
   </post>
</blogposts>

The XML data we are working on represents a fictional blogposts element. There is a blogposts root tag and it has some attributes on it and inside the blogposts, there are some posts and each post has some entries. The code extracts information from this XML as it is being parsed by the SAX parser. There are functions that will indicate that we’re starting to process the document and that we’re finishing up processing. To print out the name of the blogposts, the startElement function is used. There are also methods of endElement, characters, startDocument, and endDocument. To run the program, we place it inside of the Python main() function. A new instance of CustomContentHandler is assigned to the handler variable. Then we simply use xml.sax.parse() to read the data and print out some results.

About to start!
Blogposts title: Blog Posts Collection
Title: Parse XML With SAX
Title: Overview
Finishing up!
There were 2 post elements
There were 3 entry elements

Process finished with exit code 0

XML DOM API

Another way that XML content can be manipulated is by using the Document Object Model API or DOM. One of the big differences between the DOM API and SAX API is that the DOM allows you to access any part of the XML file at random. This is not possible with SAX as it reads one character at a time from beginning to end. With the DOM, you can also modify the XML file content. When using the DOM to parse XML code, the XML is read into memory in full and represented as a tree structure. You can then use various API’s to work on the resulting document tree. The Python Standard Library provides an implementation of the DOM API in the xml.dom.minidom module. It is intended to be a smaller implementation than the full DOM API. Below are some of the key points and methods to be aware of.

  • Access any part of the XML structure at random
  • Modify XML content
  • Represents XML as a hierarchial tree structure
  • xml.dom.minidom is a lighweight implementation
  • domtree = xml.com.minidom.parseString(str)
  • elem.getElementById(id)
  • elem.getElementsByTagName(tagname)
  • elem.getAttribute(attrName)
  • elem.setAttribute(attrName, val)
  • newElem = document.createElement(tagName)
  • newElem = document.createTextNode(strOfText)
  • elem.appendChild(newElem)

Here is an example of using xml.dom.minidom to operate on the same xmldata.xml file that we used in the SAX example. Notice this method provides a bit more flexibility and we can even add data to the file in memory. Many of us are quite familiar with the DOM since it is so common in Web Development, so working with XML in Python using the DOM is fairly easy to understand.

The root element is blogposts
The Title is: Blog Posts Collection
There are 3 entry tags
Now there are 4 entry tags

<?xml version="1.0" ?><blogposts title="Blog Posts Collection" date="A date" author="Some dude">

   <post type="summary">
      <title>Parse XML With SAX</title>
   <entry>Magic Entry!</entry></post>

   <post type="detail">
      <title>Overview</title>
      <entry>
         Parsing XML is great
      </entry>
      <entry/>
      <entry>
         Have fun with XML parsing
      </entry>
   </post>
</blogposts>

Process finished with exit code 0

XML ElementTree API

The DOM API is vast and offers cross-language and cross-platform API for working with XML data. The ElementTree API takes a different approach by focusing instead on being a simpler way of working with XML With the ElementTree API, elements are treated as if they were lists. This means that if you have an XML element that contains other elements, it is possible to iterate over those child elements using standard iteration like a for loop. The ElementTree API treats attributes like dictionaries. So if you have a reference to an element, then you can access its attrib property which is a dictionary of all the attribute names and values. ElementTree makes searching for content within XML straightforward. It offers functions that can use XPath Syntax to search the XML for specific data.

In the example below we use the ElementTree API to test these concepts out. Once again, we use the same XML data file we have been using for the entire tutorial. We can see how to build a document structure and find the root element of the tree. We can access an attribute, iterate over tags, count the number of elements, add new data, and so on.

blogposts
Blog Posts Collection
post
post
There are 2 post elements
There are 3 entry elements
There are now 3 post elements
There are now 3 entry elements

Process finished with exit code 0

Learn More About Python XML Parsing

Python XML Parsing Summary

The problem of reading, writing, and manipulating XML data in Python is solved using any of the libraries mentioned in this tutorial. We had a look at the SAX API for XML, the DOM API for XML, and lastly the ElementTree API for XML. They each have their pros can cons, and some of the links above will offer more tips and tricks for working with XML in Python.