import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.IOException;


//  A Valdating DOM Application
//  with registered Error Handlers
public class SchemaValidate implements ErrorHandler {

    // Constructor
    public SchemaValidate (String xmlFile) {

        //  Create a Xerces DOM Parser
        DOMParser parser = new DOMParser();
        //MyResolver resolver = new MyResolver();

        //  Turn Validation on
        try {
            parser.setFeature("http://xml.org/sax/features/validation", true);
            //parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "ebxml.xsd");

            //parser.setEntityResolver(resolver);
            parser.setFeature("http://apache.org/xml/features/validation/schema",true);
            parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
        }
        catch (SAXNotRecognizedException e) {
            System.err.println (e);
        }
        catch (SAXNotSupportedException e) {
            System.err.println (e);
        }

        //  Register Error Handler
        parser.setErrorHandler (this);

        //  Parse the Document
        try {
            parser.parse(xmlFile);
        }
        catch (SAXException e) {
            System.err.println (e);
        }
        catch (IOException e) {
            System.err.println (e);
        }
        catch (Exception e) {
            System.err.println (e);
        }

    }

    //  Warning Event Handler
    public void warning (SAXParseException e)
        throws SAXException {
        System.err.println ("Warning:  "+e);
    }

    //  Error Event Handler
    public void error (SAXParseException e)
        throws SAXException {
        System.err.println ("Error:  "+e);
    }

    //  Fatal Error Event Handler
    public void fatalError (SAXParseException e)
        throws SAXException {
        System.err.println ("Fatal Error:  "+e);
    }

    // Main Method
    public static void main (String[] args) {
        SchemaValidate validatingDOM = new SchemaValidate("ebxml.xml");
    }
}