Python没有捕获XMLSyntaxError

时间:2011-04-10 05:50:06

标签: python exception lxml

我有以下python + lxml代码:

def doXMLValidation (xml_file, schema_file):
    '''Validates xml file against schema'''
    s_doc = etree.parse (schema_file)
    schema = etree.XMLSchema (s_doc)
    x_file = etree.parse(xml_file)

    try:
        schema.assertValid(x_file)
    except etree.XMLSyntaxError as e:
        print (_formatXMLError(e))
        return False
    except etree.DocumentInvalid as e:
        print (_formatXMLError(e))
        return False
    except:
        print ('Something strange...')
        return False
    else:
        return True

当我尝试使用损坏的xml / fb2文件(标记已删除)进行测试时,我希望通过正确的处理获得XMLSyntaxError异常。但是,我遇到了这个错误:

  

../。元数据/ .plugins / org.python.pydev.debug / .coverage   追溯(最近的呼叫最后):
  文件   “../.eclipse/org.eclipse.platform_3.5.0_155965261/plugins/org.python.pydev.debug_1.5.9.2010063001/pysrc/coverage.py”   1029行       the_coverage.command_line(sys.argv中[1:])   文件   “../.eclipse/org.eclipse.platform_3.5.0_155965261/plugins/org.python.pydev.debug_1.5.9.2010063001/pysrc/coverage.py”   第405行,在command_line中       execfile(sys.argv [0], main ._ dict _)文件“../workspace/PythonPractice/src/lxmlValidation.py”,   第58行       test()文件“../workspace/PythonPractice/src/lxmlValidation.py”,   第54行,在测试中       result = doXMLValidation(source,schema)文件   “../workspace/PythonPractice/src/lxmlValidation.py”   第31行,在doXMLValidation中       x_file = etree.parse(xml_file)文件“lxml.etree.pyx”,第2692行,   lxml.etree.parse   (src / lxml / lxml.etree.c:49594)文件   “parser.pxi”,第1500行,in   lxml.etree._parseDocument   (src / lxml / lxml.etree.c:71364)文件   “parser.pxi”,第1529行,in   lxml.etree._parseDocumentFromURL   (src / lxml / lxml.etree.c:71647)文件   “parser.pxi”,第1429行,in   lxml.etree._parseDocFromFile   (src / lxml / lxml.etree.c:70742)文件   “parser.pxi”,第975行,in   lxml.etree._BaseParser._parseDocFromFile   (src / lxml / lxml.etree.c:67740)文件   “parser.pxi”,第539行,in   lxml.etree._ParserContext._handleParseResultDoc   (src / lxml / lxml.etree.c:63824)文件   “parser.pxi”,第625行,in   lxml.etree._handleParseResult   (src / lxml / lxml.etree.c:64745)文件   “parser.pxi”,第565行,in   lxml.etree._raiseParseError   (SRC / LXML / lxml.etree.c:64088)    lxml.etree.XMLSyntaxError:打开和结束标记不匹配:第7行和第7行,第7行,第46列

问题是什么以及如何解决?

UPD:大声笑,明白了,谢谢大家:

def doXMLValidation (xml_file, schema_file):
    '''Validates xml file against schema'''
    s_doc = etree.parse (schema_file)
    schema = etree.XMLSchema (s_doc)
    try:
        x_file = etree.parse(xml_file)
        schema.assertValid(x_file)
    except etree.XMLSyntaxError as e:
        print (_formatXMLError(e))
        return False
    except etree.DocumentInvalid as e:
        print (_formatXMLError(e))
        return False
    except:
        print ('Something strange...')
        return False
    else:
        return True

在解析时,思想lxml会更合适......

1 个答案:

答案 0 :(得分:1)

在导致错误的语句之前没有尝试过。仔细看看堆栈跟踪。