[XML-SIG] example code?
Thomas B. Passin
tpassin@idsonline.com
Sun, 19 Dec 1999 14:37:39 -0500
Tom Passin posted this code sample:
If you know all the element names you will use, do something easy like this,
which handles an element named "specialTag":
import xmllib
"""A simple class to demonstate how to handle your own elements"""
class bareBones(xmllib.XMLParser):
def __init__(self):
xmllib.XMLParser.__init__(self)
# Your element is called "specialTag"
def start_specialTag(self, attrs):
print "Start specialTag"
print "element name:", attrs
self.handle_data=self.do_data #invoke your content handler
def end_specialTag(self):
print "End specialTag"
self.handle_data=self.null_data #reset content handler
# A minimal data handler
def do_data(self,data):
print "===============\n",data,"\n==============="
def null_data(self,data):pass
doc="""<?xml version="1.0"?>
<doc>
<e1>This element won't be reported</e1>
<specialTag a="attribute 1">This one will</specialTag>
</doc>
"""
if __name__=="__main__":
parser=bareBones()
parser.feed(doc)
parser.close()
------------------------------------------------------
A minor error - I forgot to change the string printed in start_specialTag()
was:
print "element name:", attrs
should be:
print "attributes", attrs
Tom Passin