Home arrow XML arrow Page 3 - Parsing XML with SAX and Python

Parsing XML with SAX and Python

In this article Nadia explains how to parse an XML document using the SAX API implementation available for Python.

Author Info:
By: Nadia Poulou
Rating: 4 stars4 stars4 stars4 stars4 stars / 56
November 09, 2004
  1. · Parsing XML with SAX and Python
  2. · The xml.sax Package
  3. · Our SAX Parser
  4. · The Heart of the Code
  5. · Element Content
  6. · The Main Code
  7. · Homework

print this article

Parsing XML with SAX and Python - Our SAX Parser
(Page 3 of 7 )

Now, let’s put the theory of the previous section in practice. Imagine that you have the statistics of the players of a basketball team in an XML document. Let’s say the name of the document is ‘playerStats.xml’. We will build a Python script that will take a player’s name as an input and then will search the document for the player’s statistics.

Let’s say that your XML document looks something like this:

  <player name='Mick Fowler' age='27' height='1.96m'>
  <player name='Ivan Ivanovic' age='29' height='2.04m'>

As you can see, the data of the player is saved as attributes of the ‘player’ element while the match averages of points and rebounds are contents of an element.

Let’s make a Web form for the user to select one of the players. After clicking on the submit button, our script will parse the XML document return the user data together with his statistics for average points and rebounds.

The HTML code may be:

<form method="get" action="[your Python cgi script] ">
Please select a player:<br>
<select name="playerName">
<option value="Mick Fowler">Mick Fowler</option>
<option value="Ivan Ivanovic">Ivan Ivanovic</option>
<input type="submit" value="Get Player Stats">

Getting the Script to Do the Job

Now I will explain the steps necessary for our script to do the job. At the end of this section you will find the complete code.

First of all, in our code, we need to import all Python modules we will use:

from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import cgi

Since we’re dealing with a CGI script, don’t forget to put on top the path to the python executable, which is usually (on Unix systems):


Please note that the above line may vary, depending on your system configuration. Contact your system administrator for more details in case of doubt.

Also do not forget to define the content type of the CGI before any other output, so that no browsers get confused on what the content type of the page is:

 print "Content-Type: text/plain\n"

 In your script you can use the print command in order to format the output of the script in any way you want. In the example script at the end of this page you will see that I used some basic formatting. Of course, if you want your page to look really good, go ahead and change what is provided here, but do not forget that for production applications it’s usually better to try and separate your logic from your HTML code.

Although, in production applications, you may even want to structure your logic in various packages and classes, instead of doing it all in one script, as shown here. Since this is not a software engineering course, I will not get into details here but return directly to our simple script.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials