Here you can find the source of validateXML(String schemaPath, String xmlPath)
public static boolean validateXML(String schemaPath, String xmlPath)
//package com.java2s; //License from project: Open Source License import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; public class Main { public static boolean validateXML(String schemaPath, String xmlPath) { try {//w ww .j a v a 2 s . co m String schemaLang = "http://www.w3.org/2001/XMLSchema"; SchemaFactory factory = SchemaFactory.newInstance(schemaLang); // create schema by reading it from an XSD file: Schema schema = factory.newSchema(new StreamSource(schemaPath)); Validator validator = schema.newValidator(); // at last perform validation: validator.validate(new StreamSource(xmlPath)); } catch (Exception ex) { return false; } return true; } }