import org.xml.sax.*; import org.xml.sax.helpers.*; import java.io.IOException; public class DocumentStatistics { public static void main(String[] args) { XMLReader parser; try { parser = XMLReaderFactory.createXMLReader(); } catch (SAXException e) { // fall back on Xerces parser by name try { parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); } catch (SAXException ee) { System.err.println("Couldn't locate a SAX parser"); return; } } if (args.length == 0) { System.out.println( "Usage: java DocumentStatistics URL1 URL2..."); } // Install the Content Handler parser.setContentHandler(new XMLCounter()); // start parsing... for (int i = 0; i < args.length; i++) { // command line should offer URIs or file names try { parser.parse(args[i]); } catch (SAXParseException e) { // well-formedness error System.out.println(args[i] + " is not well formed."); System.out.println(e.getMessage() + " at line " + e.getLineNumber() + ", column " + e.getColumnNumber()); } catch (SAXException e) { // some other kind of error System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("Could not report on " + args[i] + " because of the IOException " + e); } } } }