Home arrow XML arrow Page 4 - 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 - The Heart of the Code
(Page 4 of 7 )

The ‘heart’ of our code is our handler class, which uses the ContentHandler object:

 class BasketBallHandler(ContentHandler):

This class has a constructor where we initialize the variables to be used in the rest of our code. Of course, we want to make sure we save the value of our search term in a variable. We also define two flags (isPointsElement and isReboundsElement) and give them the value of 0. As soon as the content of an element of interest is reached (for the ‘rebounds’ and ‘points’ elements) these flags will be set to 1.

def __init__ (self, searchTerm):

  self.searchTerm= searchTerm;
  self.isPointsElement, self.isReboundsElement = 0, 0;

I would like to mention here that all these comments and snippets of code are outlined to the left, but as you probably know already, Python is sensitive to tabbing of blocks of code. Take a look at the full code example at the end if in doubt about the correct syntax, order and tabbing.

Now we need to work with the elements of our XML document. This is why we will use the methods startElement() and endElement(). These methods will be invoked each and every time a new element in the document is, respectively, opened or closed.

The parameter name of the startElement() function contains the name of the element type. If you look again at our XML example, you will see that the ‘player’ element has some properties assigned. We obtain their values by using the get() method of the Attributes interface, then we save these values for later.

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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials