Java tutorial
//package com.java2s; import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.Document; import org.xml.sax.*; public class Main { public static Document validate(File xml, File xmlSchema) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException ex) throws SAXException { throw ex; } public void error(SAXParseException ex) throws SAXException { throw ex; } public void fatalError(SAXParseException ex) throws SAXException { throw ex; } }); return documentBuilder.parse(xml); } }