import org.apache.xerces.parsers.XMLParser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLValidator {
public static void main(String[] args) {
try {
XMLParser parser = new XMLParser();
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "schema.xsd");
DefaultHandler handler = new DefaultHandler() {
@Override
public void error(SAXParseException e) throws SAXException {
System.out.println("Error: " + e.getMessage());
}
@Override
public void warning(SAXParseException e) throws SAXException {
System.out.println("Warning: " + e.getMessage());
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("Fatal Error: " + e.getMessage());
}
};
parser.setErrorHandler(handler);
parser.parse("data.xml");
System.out.println("XML validation successful.");
} catch (Exception e) {
e.printStackTrace();
}
}
}