问题描述
private void validateXML(DOMSource source) throws Exception {
URL schemaFile = new URL("http://www.csc.liv.ac.uk/~valli/modules.xsd");
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
DOMResult result = new DOMResult();
try {
validator.validate(source, result);
System.out.println("is valid");
} catch (SAXException e) {
System.out.println("not valid because " + e.getLocalizedMessage());
}
}
但这会返回一个错误:线程main"java.lang.IllegalArgumentException 中的异常:没有实现由 http:///www.w3.org/2001/XMLSchema - 可以加载实例
But this returns an error saying: Exception in thread "main" java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/2001/XMLSchema -instance could be loaded
这是我的代码问题还是实际 xsd 文件的问题?
Is this a problem with my code or with the actual xsd file?
推荐答案
这个错误意味着你安装的Java没有任何可以解析XMLSchema文件的类,所以不是schema或者你的代码有问题.
That error means that your installed Java doesn't have any classes that can parse XMLSchema files, so it's not a problem with the schema or your code.
p>
我很确定最近的 JRE 默认有合适的类,所以你能给我们提供 java -version
的输出吗?
更新:
您使用了错误的 XMLContants 字符串.你想要:XMLConstants.W3C_XML_SCHEMA_NS_URI
You're using the wrong XMLContants string. You want: XMLConstants.W3C_XML_SCHEMA_NS_URI
这篇关于针对 XSD Schema 的 Java XML 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!