Turn of validation for a SAXParserFactory

2019-03-04 15:50发布

问题:

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 occurence 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 arround 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).