Turn off validation for a SAXParserFactory

我的梦境 提交于 2019-12-20 06:56:27

问题


On a java application using XML, some tests must be performed with the XML validation disabled in order to ensure that the java parts behave correctly if they face incorrect data. We also need it in order to check older methods (written more than 10 years ago).

I tried to locate every occurrence of a SAXParserFactory and use

setValidation(false);

on it, in order to disable the validation. Unfortunately, I keep getting errors like the one linked below.

I have been playing around with this for a while now, and can't relate to similar cases like this. The elements triggering the exception are similar to this :

Exception=org.xml.sax.SAXException, cvc-complex-type.4=Attribute 'date' must appear on element 'XXXXXXXXX'

Any ideas on the proper way to disable the validation correctly ?

Thanks for reading.


回答1:


I guess you mean SAXParserFactory#setValidating(boolean value). However, that method controls whether the parser validates the XML document according to a DTD or not. In your stack trace it looks like schemas are used instead.

To avoid validatation against schemas, you should look for occurrences of SAXParserFactory#setSchema(...) and do whatever necessary (remove/comment out).



来源:https://stackoverflow.com/questions/40992051/turn-off-validation-for-a-saxparserfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!