如何使用Python库验证XML文件时出现多个错误? [英] How to get multiple errors validating XML file with Python libraries?

查看:96
本文介绍了如何使用Python库验证XML文件时出现多个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要验证的XML文件,并且必须使用Python进行处理.我试图用带有lxml的XSD对其进行验证.但是我只收到一个首先发生的错误,但是我需要XML文件中的所有错误和不匹配项.有什么方法可以管理lxml来获取所有错误的列表?还是还有其他Python解决方案?

I have some XML file i want to validate and i have to do it with Python. I've tryed to validate it with XSD with lxml. But i get only one error which occurs first but i need all errors and mismatches in XML file. Is there any method how i can manage to get list of all errors with lxml? Or are there any other Python solutions?

推荐答案

解决此问题的方法是:

try:
    xmlschema.assertValid(xml_to_validate)
except etree.DocumentInvalid, xml_errors:
    pass
print "List of errors:\r\n", xml_errors.error_log

也许有更好的方法来解决这个问题:)

May be there are better ways to solve this problem :)

这篇关于如何使用Python库验证XML文件时出现多个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆