Java XML validation against XSD Schema(针对 XSD Schema 的 Java XML 验证)
问题描述
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 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:针对 XSD Schema 的 Java XML 验证
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
