【发布时间】:2015-08-28 13:05:14
【问题描述】:
我有一个 xml,我验证它是否真的是这样一个格式良好的 xml:
try:
self.doc=etree.parse(attributesXMLFilePath)
except IOError:
error_message = "Error: Couldn't find attribute XML file path {0}".format(attributesXMLFilePath)
raise XMLFileNotFoundException(error_message)
except XMLSyntaxError:
error_message = "The file {0} is not a good XML file, recheck please".format(attributesXMLFilePath)
raise NotGoodXMLFormatException(error_message)
如您所见,我遇到了 XMLSyntaxError,这是来自:
的错误from lxml.etree import XMLSyntaxError
效果很好,但这只是告诉我文件是否不是好的 xml 格式。但是,我想问你们是否有办法知道哪个标签是错误的,因为在我这样做时:
<name>Marco</name1>
我收到了错误,有没有办法知道name标签还没有关闭?
更新
在有人给我线和位置的想法之后,我想出了这个代码:
class XMLFileNotFoundException(GeneralSpiderException):
def __init__(self, message):
super(XMLFileNotFoundException, self).__init__(message, self)
class GeneralSpiderException(Exception):
def __init__(self, message, e):
super(GeneralSpiderException, self).__init__(message+" \nline of Exception = {0}, position of Exception = {1}".format(e.lineno, e.position))
我仍然像这样提出错误
raise XMLFileNotFoundException(error_message)
我现在遇到了这个错误
super(GeneralSpiderException, self).__init__(message+" \nline of Exception = {0}, position of Exception = {1}".format(e.lineno, e.position))
exceptions.AttributeError: 'XMLFileNotFoundException' object has no attribute 'lineno'
【问题讨论】: