Search code examples
javaxmlvalidationsax

Turn off validation for a SAXParserFactory


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.

SAXException stacktrace

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.


Solution

  • 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).