XML文件的验证模式保证了XML文件的正确性,而比较常用的验证模式有两种:DTD和XSD两种。
使用DTD验证模式需要在XML文件的头部声明。
使用XSD除了要声明名称空间外,必须指定该名称空间所对应的XML Schema文件的存储位置。
getValidationModeForResource()方法,如下
protected int getValidationModeForResource(Resource resource) {
int validationModeToUse = getValidationMode();
//如果手动指定了验证模式则使用指定的验证模式
if (validationModeToUse != VALIDATION_AUTO) {
return validationModeToUse;
}
//如果没有指定则使用自动检测
int detectedMode = detectValidationMode(resource);
if (detectedMode != VALIDATION_AUTO) {
return detectedMode;
}
// Hmm, we didn't get a clear indication... Let's assume XSD,
// since apparently no DTD declaration has been found up until
// detection stopped (before finding the document's root tag).
return VALIDATION_XSD;
}
网友评论