在网络上找了,还是使用的sax,确实比较方便,组装成如下的函数,要使用的直接调用就好了,
如果xml文件正确,那么函数返回True,
如果xml文件不正确,那么函数返回False,
from xml.sax.handler import ContentHandler
from xml.sax import make_parser
def parse_File(fileName):
#判断XML文件的正确性
xml_ok=True
try:
parser = make_parser()
parser.setContentHandler(ContentHandler())
parser.parse(fileName)
xml_ok=True
except Exception as e:
xml_ok=False
print('\n\t:(, Error found in file:%s\n' % e)
return xml_ok
if __name__ == "__main__":#初始化内容
abc=True
# fileName文件名字,需自己定义
abc=parse_File(fileName)
```