import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXParseException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.ErrorHandler; import java.io.File; import java.io.IOException; public class SchmValidateDOM implements ErrorHandler { public static void main(String[] args) { SchmValidateDOM sv = new SchmValidateDOM(); sv.parseFile(args[0]); } private void parseFile(String filename) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(true); dbf.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); docBuilder.setErrorHandler(this); Document document = docBuilder.parse(new File(filename)); } catch(ParserConfigurationException pce) { pce.printStackTrace(); } catch(SAXException se) { se.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } } public void error(SAXParseException exception) throws SAXException { System.out.println("Line: "+exception.getLineNumber()+", Column: "+exception.getColumnNumber()+", Message: "+exception.getMessage()); } public void fatalError(SAXParseException exception) throws SAXException { System.out.println("Line: "+exception.getLineNumber()+", Column: "+exception.getColumnNumber()+", Message: "+exception.getMessage()); } public void warning(SAXParseException exception) throws SAXException { System.out.println("Line: "+exception.getLineNumber()+", Column: "+exception.getColumnNumber()+", Message: "+exception.getMessage()); } }