XML Python Tutorial

from xml.sax.handler  import ContentHandler
from xml.sax import parse
class PageMaker(ContentHandler):
    passthrough = 0
    def startElement(self, name, attrs):
        if name == 'page':
            self.passthrough = 1
            self.out = open(attrs['name'] + '.html', 'w')
            self.out.write('\n')
            self.out.write('%s\n' % attrs['title'])
            self.out.write('\n')
        elif self.passthrough:
            self.out.write('<' + name)
            for key, val in attrs.items():
                self.out.write(' %s="%s"' % (key, val))
            self.out.write('>')
    def endElement(self, name):
        if name == 'page':
            self.passthrough = 0
            self.out.write('\n\n')
            self.out.close()
        elif self.passthrough:
            self.out.write('' % name)
    def characters(self, chars):
        if self.passthrough: self.out.write(chars)
parse('website.xml', PageMaker ())