XML验证-使用多个xsd [英] XML Validation - Using multiple xsd's

查看:79
本文介绍了XML验证-使用多个xsd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个xsd文件来验证xml.但是问题是我的代码只需要一个xsd.如何在以下代码中使用其他xsd?我不知道应该在哪里放置/调用第二个xsd文件.

I have two xsd files to validate a xml. But the problem is my code takes only one xsd. How to use other xsd in the following code? I dont have idea about where should i place/call 2nd xsd file.

             private void validate(File xmlF,File xsd1,File xsd2) {
                    try {
                        url = new URL(xsd.toURI().toString());//  xsd1
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }


                    source = new StreamSource(xml); // xml
                    try {
                        System.out.println(url);
                        schema = schemaFactory.newSchema(url);
                    } catch (SAXException e) {
                        e.printStackTrace();
                    }
                    validator = schema.newValidator();
                    System.out.println(xml);
                    try {
                        validator.validate(source);
                    } catch (SAXException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

推荐答案

在SO或Google上搜索时点击率很高.其中一个是问题,其中作者找到了自己的解决方案,并报告了以下代码,以将多个xsd添加到验证器中:

Plenty of hits when searching on SO or Google. One of them is this question, where the author found his own solution and reports the following code to add multiple xsd's to the validator:

Schema schema = factory().newSchema(new Source[] {
  new StreamSource(stream("foo.xsd")),
  new StreamSource(stream("Alpha.xsd")),
  new StreamSource(stream("Mercury.xsd")),
});

但是,当直接使用 StreamSource 上的 InputStream 时,解析器无法加载任何引用的XSD文件.例如,如果文件 xsd1 导入或包含第三个文件(不是 xsd2 ),则架构创建将失败.您应该设置系统标识符( setSystemId )或(甚至更好)使用 StreamSource(File f)构造函数.

However, when working directly with InputStream on StreamSource, the resolver is not able to load any referenced XSD files. If, for instance, the file xsd1 imports or includes a third file (which is not xsd2), schema creation will fail. You should either set the system identifier (setSystemId) or (even better) use the StreamSource(File f) constructor.

调整为您的示例代码:

try {
  schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  schema = schemaFactory.newSchema(new Source[] {
    new StreamSource(xsd1), new StreamSource(xsd2)
  });
} catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

注意:

如果使用类路径资源,我希望使用 StreamSource(String systemId)构造函数(而不是创建 File ):

If working with classpath resources, I'd prefer the StreamSource(String systemId) constructor (rather than creating a File):

new StreamSource(getClass().getClassLoader().getResource("a.xsd").toExternalForm());

这篇关于XML验证-使用多个xsd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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