[XML-SIG] Trying to get Metaparser code segment working

Yusuf Goolamabbas yusufg@outblaze.com
13 Mar 2000 06:22:57 -0000


hi, My apologies if this question is not relevant to this SIG. I am a
hobbyist Python programmer and wanted to see what the state of XML
processing in python was. I read various websites and downloaded PyXML
and got it working with Python 1.5.2. I came across Fourthoughts
presentation on XML/Python and was quite intrigued by the Metaparser
usage of XML

http://fourthought.com/Presentations/oreilly-1999-08-22/page2-4.html

I wrote a simple program combing this with the previous example but
keep on getting exception errors. I read through Learning Python but
wasn't sure if exec can call functions. Would appreciate any pointers
to where I goofed up

Thanks, Yusuf
--
Yusuf Goolamabbas
yusufg@outblaze.com


#!/usr/bin/python

import sys
from xml.sax import saxlib, saxexts
class MetaMagicParser(saxlib.HandlerBase):
	""" MetaMagicParser handler class"""
	
	def __init__(self):
		self.print_flag = 0
	# Constructor

	def startElement(self, name, attribs):
		exec('start_' + name + '(' + "self" + ',' + "attribs" + ')')

	def start_ADDRBOOK(self, attribs):
		sys.stdout.write('Addrbook: ')

	def start_ENTRY(self, attribs):
		sys.stdout.write('Entry: ')
	
	def start_NAME(self, attribs):
		sys.stdout.write('Name: ')

	def start_ADDRESS(self, attribs):
		sys.stdout.write('Address: ')

	def start_EMAIL(self, attribs):
		sys.stdout.write('Email: ')
	 
	def start_PHONENUM(self, attribs):
		sys.stdout.write('Phonenum: ')


p = saxexts.XMLValParserFactory.make_parser()
p.setDocumentHandler(MetaMagicParser())
xml_file = open(sys.argv[1], 'r')
p.parseFile(xml_file)
xml_file.close()

Error I am getting is

Traceback (innermost last):
  File "./metaparser.py", line 41, in ?
    p.parseFile(xml_file)
  File "/usr/lib/python1.5/site-packages/xml/sax/drivers/drv_xmlproc.py", line 28, in parseFile
    self.parser.read_from(file)
  File "/usr/lib/python1.5/site-packages/xml/parsers/xmlproc/xmlval.py", line 98, in read_from
    self.parser.read_from(file)
  File "/usr/lib/python1.5/site-packages/xml/parsers/xmlproc/xmlutils.py", line 138, in read_from
    self.feed(buf)
  File "/usr/lib/python1.5/site-packages/xml/parsers/xmlproc/xmlutils.py", line 183, in feed
    self.do_parse()
  File "/usr/lib/python1.5/site-packages/xml/parsers/xmlproc/xmlproc.py", line 83, in do_parse
    self.parse_start_tag()
  File "/usr/lib/python1.5/site-packages/xml/parsers/xmlproc/xmlproc.py", line 185, in parse_start_tag
    self.app.handle_start_tag(name,attrs)
  File "/usr/lib/python1.5/site-packages/xml/parsers/xmlproc/xmlval.py", line 160, in handle_start_tag
    self.realapp.handle_start_tag(name,attrs)
  File "/usr/lib/python1.5/site-packages/xml/sax/drivers/drv_xmlproc_val.py", line 31, in handle_start_tag
    self.doc_handler.startElement(name,XPAttributes(attrs,None))
  File "./metaparser.py", line 17, in startElement
    exec('start_' + name + '(self, attribs)')
  File "<string>", line 1, in ?
NameError: start_ADDRBOOK