针对XSD架构的Java XML验证 [英] Java XML validation against XSD Schema

查看:111
本文介绍了针对XSD架构的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:没有实现指定的模式语言的SchemaFactory: http:/ /www.w3.org/2001/XMLSchema -instance可以加载

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文件的类,因此它不是架构的问题或者你的代码。

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.

我很确定最近的JRE默认有相应的类,所以你能得到 java -version的输出吗?

I'm pretty sure recent JREs have the appropriate classes by default, so can you get us the output of 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架构的Java XML验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆