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.

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

             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();
                    }
                }

1 个解决方案

#1


22

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:

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

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

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.

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

Adjusted to your example code:

调整为您的示例代码:

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();
}

Note:

注意:

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

如果使用类路径资源,我更喜欢StreamSource(String systemId)构造函数(而不是创建文件):

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

更多相关文章

  1. 反编译APK 得到JAVA代码和资源文件源码
  2. java.io.File vs java.nio.Files这是新代码中的首选?
  3. JSP页面中有一个按钮 点击之后执行JAVA文件
  4. 如何在Java中递归解压缩文件?
  5. Java获得文件的创建时间(精确到秒)
  6. 拖动层的javasvript代码 十行代码即可写出兼容版拖动层
  7. 安卓Android文件上传Demo报错 java.io.FileNotFoundException: /
  8. Eclipse创建的包变成文件夹的解决方法
  9. 我无法让这个简单的ajax代码工作

随机推荐

  1. Golang 能不能打包为 dll ?
  2. golang可以写网站吗
  3. Golang 免费的吗?
  4. 区块链实战-Hyperledger Fabric(一) 10分钟
  5. Golang 哪个 Web 框架好用?
  6. golang可以做些什么?
  7. Golang 是面向对象还是面向过程?
  8. golang吉祥物是啥?
  9. Golang 和 PHP 哪个性能更强?
  10. golang会代替java吗?