This parses the file, using registered SAX handlers, and output
the events in the parsing process cycle.
System.out.println("Parsing XML File: " + uri + "\n\n");
// Get instances of our handlers
ContentHandler contentHandler = new MyContentHandler();
ErrorHandler errorHandler = new MyErrorHandler();
try {
// Instantiate a parser
XMLReader parser =
XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
// Register the content handler
parser.setContentHandler(contentHandler);
// Register the error handler
parser.setErrorHandler(errorHandler);
// Turn on validation
parser.setFeature("http://xml.org/sax/features/validation",
true);
// Turn off namespace awareness
parser.setFeature("http://xml.org/sax/features/namespaces",
false);
// Parse the document
parser.parse(uri);
} catch (IOException e) {
System.out.println("Error reading URI: " + e.getMessage());
} catch (SAXException e) {
System.out.println("Error in parsing: " + e.getMessage());
}