!--a11y-->
Using XML Schema with SAX 
If you use SAX to parse your XML, you can use the http://apache.org/xml/features/validation/schema feature to signal the parser that you intend to validate the XML against an XML Schema definition. The location of the schema definition can be provided in the following ways:
If you are not sure whether the XML document will contain a reference to a schema, you can use the dynamic validation feature - http://apache.orgxml/features/validation/dynamic, which turns validation on, only if there is a schema location specified.

|
import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.XMLReaderFactory;
public class SchemaThroughSAXExample { public static void main(String[] args) throws Exception { String xsdFile = "data/pair.xsd"; String xmlFile = "data/pair.xml"; XMLReader parser = XMLReaderFactory.createXMLReader("com.sap.engine.lib.xml.parser.SAXParser"); parser.setProperty( "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" , xsdFile); parser.setFeature( "http://xml.org/sax/features/validation" , true); parser.setFeature( "http://xml.org/sax/features/namespaces" , true); parser.setFeature( "http://apache.org/xml/features/validation/dynamic" , true); parser.setFeature( "http://apache.org/xml/features/validation/schema" , true); System.out.println(parser.parse( new InputSource(xmlFile))); } } |
