問題描述
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 驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!