
import java.io.File;

import org.apache.xerces.util.XMLGrammarPoolImpl;
import org.apache.xerces.xni.grammars.XMLGrammarPool;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

 

public class SaxParser2
{

	XMLGrammarPool grammarPool = new XMLGrammarPoolImpl();
	 
	public void validateXMLForDTD(File file)
	{
		try
		{
			XMLReader parser = XMLReaderFactory
					.createXMLReader("org.apache.xerces.parsers.SAXParser");
			 
				 parser
				 		.setProperty(
				 				"http://apache.org/xml/properties/internal/grammar-pool",
				 			grammarPool);
			 
			parser.setErrorHandler(new ErrorHandler()
			{
				public void warning(SAXParseException saxE) throws SAXException
				{

					System.out.println(this.getClass() + " handlWarning "
							+ saxE.getMessage());
				}

				public void error(SAXParseException saxE) throws SAXException
				{
					System.out.println(this.getClass() + " handleError "
							+ saxE.getMessage());
				}

				public void fatalError(SAXParseException saxE)
						throws SAXException
				{

					System.out.println(this.getClass() + " handleFatal "
							+ saxE.getMessage());
				}
			});
			parser.setFeature("http://xml.org/sax/features/validation", true);
			InputSource input = new InputSource();
			input.setSystemId(file.getAbsolutePath());
			 
			parser.parse(input);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		
		}
	
	}

	public static void main(String[] args)
	{
		SaxParser2 parser = new SaxParser2();
		try
		{
			System.out.println("===first time");
			parser.validateXMLForDTD(new File("note2.xml"));
			System.out.println("===second time");
			parser.validateXMLForDTD(new File("note2.xml"));
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
