XML
  Home arrow XML arrow Page 4 - Parsing XML with SAX and Python
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Parsing XML with SAX and Python
By: Nadia Poulou
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 27
    2004-11-09

    Table of Contents:
  • Parsing XML with SAX and Python
  • The xml.sax Package
  • Our SAX Parser
  • The Heart of the Code
  • Element Content
  • The Main Code
  • Homework

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More XML Articles
    More By Nadia Poulou


       · Nice article. The correct URL for Uche Ogbuji's site...
       · I'm new to all of this and found this article to be a tremendous help. Thanks so...
       · Thanks for this great article. I have a question. I am using your code to learn xml...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT