python类库31[使用SAX来解析xml]
实例:
from xml.sax import handler, make_parser
class TestHandler(handler.ContentHandler):
def __init__(self):
pass
def startDocument(self):
print "Document Start ..."
def endDocument(self):
print "Document End ..."
def startElement(self, name, attrs):
print 'start tag:', name
def endElement(self, name):
print 'end tag:', name
def characters(self, chrs):
print chrs
def test():
handler = TestHandler()
parser = make_parser()
parser.setContentHandler(handler)
f = open(sys.argv[1], 'r')
parser.parse(f)
f.close()
if __name__ == '__main__':
test()
除了DOM外的另一种读写文件的方式。
完!