// Create a new factory to create parsers that will
// be aware of namespaces and will validate or
// not according to the flag setting.
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
// Use the factory to create a parser (builder) and use
// it to parse the document. try {
DocumentBuilder builder = dbf.newDocumentBuilder();
builder.setErrorHandler(new MyErrorHandler());
InputSource is = new InputSource(filename);
Document doc = builder.parse(is);
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
} private static void usage() {
System.err.println("Usage: DOMCheck [-v] <filename>");
System.exit(1);
}
} class MyErrorHandler implements ErrorHandler { public void warning(SAXParseException e) throws SAXException {
show("Warning", e); throw (e);
}