The following code shows you how to use the DOMImplementationLS and DOMConfiguration classes to validate an XML document only when an XML Schema is present:
DOMImplementationLS DOMiLS=null;
//get a LSParser
LSParser LSP=DOMiLS.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS,
"http://www.w3.org/2001/XMLSchema");
//get a DOMConfiguration
DOMConfiguration DOMc=LSP.getDomConfig();
//validating
if(DOMc.canSetParameter("schema-type","http://www.w3.org/2001/XMLSchema"))
DOMc.setParameter("schema-type","http://www.w3.org/2001/XMLSchema");
else System.out.println("Error...");
//use validate-if-schema
if(DOMc.canSetParameter("validate-if-schema",true))
DOMc.setParameter("validate-if-schema",true);
else System.out.println("Error...");