java XML 验证 - 使用多个 xsd

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7009285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:15:34  来源:igfitidea点击:

XML Validation - Using multiple xsd's

javaxmlvalidation

提问by freepublicview

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

回答by Wivani

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

在 SO 或 Google 上搜索时有很多点击。其中之一是thisquestion,作者在那里找到了自己的解决方案并报告了以下代码以将多个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 InputStreamon StreamSource, the resolver is not able to load any referenced XSD files. If, for instance, the file xsd1imports 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.

但是,当直接使用InputStreamon 时StreamSource,解析器无法加载任何引用的 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)构造函数(而不是创建一个File):

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