XML documentation stinks - help?

Simon John simoninusa2001 at yahoo.co.uk
Wed Sep 1 19:08:47 CEST 2004

The way I currently use XML in Perl is that you know certain tags, and
then look for any data within them. E.g. you have:

sub session_end_handler {
if ($tag eq 'session_details') {
my $session_id = $xml{'session_id'};
my $username = $xml{'username'}

But with the XML examples I've managed to get going with Python, it
seems to work in reverse - you need to know the data you're looking for

from xml.sax import ContentHandler, parseString

xmlsource = """<collection>
<comic title="Sandman" number='62'>
<writer>Neil Gaiman</writer>
<penciller pages='1-9,18-24'>Glyn Dillon</penciller>
<penciller pages="10-17">Charles Vess</penciller>

class docHandler(ContentHandler):
"""overload contenthandler"""
def __init__(self, title, number):
self.search_title, self.search_number = title, number

def startElement(self, tag, attribs):
print "tag:", tag
title = attribs.get('title', None)
number = attribs.get('number', None)
print "title:", title
print "number:", number

# create instance of dochandler class
handler = docHandler('Sandman', '62')
# parse the xml
parseString(xmlsource, handler)

More information about the Python-list mailing list