有没有办法将参数传递给SAX解析器。我按如下方式获得了解析器:
from xml.sax import make_parser, handler
class parserSAXHandler(handler.ContentHandler):
def __init__(self):
def startElement(self, name, attrs):
def endElement(self,name):
def characters(self, content):
parser = make_parser()
parser.setContentHandler(parserSAXHandler())
doc="PathToDocToBeParsed";
parser.parse(doc);
如何将参数传递给处理程序对象?
答案 0 :(得分:2)
只需让你的__init__
函数接受参数,并将这些参数存储到以后。
from xml.sax import make_parser, handler
class parserSAXHandler(handler.ContentHandler):
def __init__(self, something):
self.something = something
def startElement(self, name, attrs):
def endElement(self,name):
...
do_something(self.something)
...
def characters(self, content):
parser = make_parser()
parser.setContentHandler(parserSAXHandler(some_argument))
doc="PathToDocToBeParsed";
parser.parse(doc);