python 使用SAX解析器验证xml文件格式是否完好

#!/usr/bin/env python  
#-*- coding: UTF-8 -*- 
#coder:wangrui

#验证xml文件格式是否正确
from xml.sax.handler import ContentHandler
from xml.sax import make_parser

def parseFile(fileName):
    parser = make_parser()
    parser.setContentHandler(ContentHandler())
    parser.parse(fileName)
    
            
if __name__ == '__main__':
    filename = 'test.xml'
    try:
        parseFile(filename)
        print '%s is well-formed' %filename
    except Exception, e:
        print '%s is not well-formed %s' %(filename, e)

你可能感兴趣的:(exception,xml,python,import)