import java.io.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.ext.LexicalHandler; class SAXCatch { static public void main(String[] args) throws Exception { XMLReader xmlReader = null; try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); xmlReader = saxParser.getXMLReader(); } catch (FactoryConfigurationError fce) { System.err.println( "can't configure parser" ); } catch (ParserConfigurationException pce) { System.err.println( "can't provide parser" ); } catch (SAXException sxe) { System.err.println( sxe.getMessage () ); } if( xmlReader != null ){ try { xmlReader.setProperty( "http://xml.org/sax/properties/lexical-handler", new MyLexicalHandler() ); } catch (SAXNotSupportedException nse) { System.out.println( "parser can't set property" ); } catch (SAXNotRecognizedException nre) { System.out.println( "unrecognized property" ); } try { xmlReader.parse( new File(args[0]).toURL().toString() ); } catch (SAXParseException pe) { System.out.println( "Error at " + pe.getSystemId() + " line " + pe.getLineNumber() + ": " + pe.getMessage() ); } catch (SAXException se) { // other SAX errors System.out.println( "ill-formed XML file" + se.getMessage()); } catch (IOException e) { System.out.println( "can't find/read file" ); } catch (Throwable t) { t.printStackTrace(); } // other errors } } }