XML
  Home arrow XML arrow Page 3 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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: 4 stars4 stars4 stars4 stars4 stars / 34
    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 - 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:

    <team>
      <player name='Mick Fowler' age='27' height='1.96m'>
        <points>17.1</points>
        <rebounds>6.4</rebounds>
      </player>
      <player name='Ivan Ivanovic' age='29' height='2.04m'>
        <points>15.5</points>
        <rebounds>7.8</rebounds>
      </player>
    </team>

    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>
    </select><br>
    <input type="submit" value="Get Player Stats">
    </form>

    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):

    #!/usr/bin/python

    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.

    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...
       · Thank you for this article. Very easy, ideas very well exposed. The code have...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT