import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* @author Administrator
* 根据Schema xsd文件验证 xml 文件
*/
public class XmlValidator {
/**
* 根据Schema xsd文件验证 xml 文件
* @param xmlPath:xml 文件的路径
* @param xsdPath:xsd 文件的路径
* @return:返回验证结果
*/
public static boolean validate(String xmlPath, String xsdPath) {
boolean flag = false;
//获取当前工程的 classpath 的 绝对路径uri
String basePath = ().getContextClassLoader().getResource("").getPath();
//basePath = /D:/Eclipse/eclipse/SalaryManager/build/classes/
//构建绝对路径
xmlPath = basePath + xmlPath;
xsdPath = basePath + xsdPath;
try {
//查找支持指定模式语言的 SchemaFactory 的实现并返回它
SchemaFactory factory =
("http:///2001/XMLSchema");
//解析作为模式的指定 File 并以 Schema 的形式返回它
//此对象表示可以根据 XML 文档检查/实施的约束集
File file = new File(xsdPath);
Schema schema = (file);
//验证器实施/检查此对象表示的约束集。Validator -> 根据 Schema 检查 XML 文档的处理器。
Validator validator = ();
//验证指定的输入。 Source -> 实现此接口的对象包含充当源输入(XML 源或转换指令)所需的信息
Source source = new StreamSource(xmlPath);
(source);
flag = true;
} catch (SAXException e) {
();
} catch (IOException e) {
();
}
return flag;
}
}