SAXParserFactory XML Schema Validation

With the schemalocation specified in the XML Document using the schemaLocation or noNamespaceSchemaLocation attributes.

try {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setNamespaceAware( true);
  factory.setValidating( true);

  SAXParser parser = factory.newSAXParser();
  parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
                      "http://www.w3.org/2001/XMLSchema");

  XMLReader reader = parser.getXMLReader();
  reader.parse( new Inputsource( "test.xml"));

} catch ( ParserConfigurationException e) {
  e.printStackTrace();
} catch ( SAXException e) {
  e.printStackTrace();
} catch ( IOException e) {
  e.printStackTrace();
}
			

With the schemalocation specified by the 'http://java.sun.com/xml/jaxp/properties/schemaSource' property.

try {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setNamespaceAware( true);
  factory.setValidating( true);

  SAXParser parser = factory.newSAXParser();
  parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
                      "http://www.w3.org/2001/XMLSchema");
  parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource", 
                      "file:test2.xsd");

  XMLReader reader = parser.getXMLReader();
  reader.parse( new Inputsource( "test.xml"));

} catch ( ParserConfigurationException e) {
  e.printStackTrace();
} catch ( SAXException e) {
  e.printStackTrace();
} catch ( IOException e) {
  e.printStackTrace();
}