【问题标题】:python how to know which tag exactly is not closed in xmlpython如何知道哪个标签在xml中没有关闭
【发布时间】: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'

【问题讨论】:

标签: python xml lxml


【解决方案1】:

您可以打印错误的详细信息。例如:

try:
    self.doc = etree.parse(attributesXMLFilePath)
except XMLSyntaxError as e:
    error_message = "The file {0} is not correct XML, {1}".format(attributesXMLFilePath, e.msg)
    raise NotGoodXMLFormatException(error_message)

【讨论】:

  • 这听起来不错,但我有(如您所见)一个自定义异常,我尝试在我的自定义异常中执行 self.lineno,但出现错误,即无法识别 lineno,
  • @MarcoDinatsoli 我编辑了答案以将变量传递给自定义异常
  • 所以你是在告诉我每次提出异常时我都必须通过行数和位置对吗?
  • 我不太了解自定义异常,可以肯定地告诉你。最初,我认为您想要的只是获取该特定错误的详细信息。顺便说一句,您的编辑都是关于XMLFileNotFoundException,但我认为应该是NotGoodXMLFormatException
  • 这两个例外是一样的,我问的是思路,大致思路,而不是具体的例外
【解决方案2】:

这可能不是您想要的,但您可以从异常中获取检测到错误的确切行和列:

import lxml.etree
import StringIO
xml_fragment = "<name>Marco</name1>"
#               12345678901234
try:
    lxml.etree.parse(StringIO.StringIO(xml_fragment))
except lxml.etree.XMLSyntaxError as exc:
    line, column = exc.position

在本例中,linecolumn 将分别为 1 和 14,表示没有匹配的开始标签的结束标签的第一个字符。

【讨论】:

  • 这听起来不错,但我有(如你所见)一个自定义异常,我尝试在我的自定义 excpeiont 中执行 self.lineno,但出现错误,即无法识别 lineno class XMLFileNotFoundException(GeneralSpiderException): def __init__(self, message): print self.lineno super(XMLFileNotFoundException, self).__init__(message, self)
  • 什么是GeneralSpiderException?是否继承自XMLSyntaxError
  • 这对我来说是另一个例外。你不应该关心它,它继承了Exception
  • 我的意思是这个想法,我在另一个异常 NotGoodXMLFormatException 中做了同样的打印
  • 我不确定您的自定义异常与lxml.tree.parse 引发的异常有什么关系。也许您想捕获语法错误,使用该信息来识别元素,并将 that 信息放入自定义异常中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-14
  • 2022-01-21
  • 2010-11-30
  • 2010-10-23
  • 2020-11-15
  • 1970-01-01
相关资源
最近更新 更多