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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Parsing XML with SAX and Python - Homework
(Page 7 of 7 )

Even if your knowledge of XML and Python is not advanced, I hope that by now you have an idea of how to implement a simple XML parser in Python. Of course this was just a very simple example to illustrate the possibilities of the xml.sax module of Python.

Here follow some ideas, based on our example, for further exploration:

  • Try to load the <select> list on our Web form dynamically from the XML document; that is to parse the document and to present the player names found as elements of the list.

  • In our example we mentioned nothing about proper XML syntax and similar matters. In production applications things do not always turn out as we would hope. This is why exception handling should always be part of your application. You can try to use one or more exception classes to handle the behavior of your script as soon as an error occurs, by (for example) showing the appropriate error message.

Conclusion 

This tutorial covered the basics of the xml.sax package and its sub packages. This is not the only way to parse XML using Python. Hopefully in future articles we can take a look at other ways to do so and also take into account matters like XML validation and namespaces.

For the moment I would like to mention here PyXML, a toolkit with more than 200 XML-related modules and advanced implementations, including the xml.sax packages and sub packages. You can download it from here.

A very good reference site for Python and XML is "Uche Ogbuji's Akara site for XML processing in Python".

Also, you may want to check out the Python library reference (search for the xml.sax package and sub-packages)


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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