/** * Example of an error handler which prints out the error message * along with line and column number * * @author Nauman Chaudhry * 04/09/07 */ import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class Ex3ErrorHandler extends DefaultHandler { public static void main(String[] args){ try{ XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setFeature(VALIDATION_FEATURE_URI, true); Ex3ErrorHandler handler = new Ex3ErrorHandler(); xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(handler); xmlReader.parse(args[0]); }catch(Exception e){ e.printStackTrace(); } } public void warning(SAXParseException e) throws SAXException { System.out.println("***** Warning: " + e.getMessage() + " at: " + e.getLineNumber() + " " + e.getColumnNumber()); } public void error(SAXParseException e) throws SAXException { System.out.println("***** Error: " + e.getMessage() + " at: " + e.getLineNumber() + " " + e.getColumnNumber()); } public void fatalEror(SAXParseException e) throws SAXException { System.out.println("****** FatalError: " + e.getMessage() + " at: " + e.getLineNumber() + " " + e.getColumnNumber()); } public void startDocument() throws SAXException{ System.out.println("Start document"); } public void endDocument() throws SAXException{ System.out.println("End document"); } public void startElement(String namespaceURI, String localName, String qualName, Attributes atts){ System.out.println("startElement:\t" + qualName); } public void endElement(String namespaceURI, String localName, String qualName){ System.out.println("endElement:\t" + qualName); } public void characters(char[] charData, int start, int length) throws SAXException { System.out.println("content:\t" + new String(charData, start, length)); } private static String VALIDATION_FEATURE_URI = "http://xml.org/sax/features/validation"; }