xml与xsd的匹配性检测示例
import java.io.file;
import javax.xml.transform.source;
import javax.xml.transform.stream.streamsource;
import javax.xml.validation.schemafactory;
public class xmlhandler {
public static final string xsd_file_path = "automation.xsd";
public static void checkxsd(file xmlfile) {
schemafactory schemafactory = schemafactory.newinstance("http://www.w3.org/2001/xmlschema");
try {
schemafactory.newschema(
new source[] {
new streamsource(
thread.currentthread().getcontextclassloader().getresourceasstream(xsd_file_path))}
).newvalidator().validate(new streamsource(xmlfile));
} catch (exception e) {
throw new runtimeexception("xsd检测失败",e);
}
}
}
不抛异常表示检测通过。
以上就是xsd与xml的检测示例代码的详细内容。