【问题标题】:Get pathname to file from Maven resources从 Maven 资源获取文件的路径名
【发布时间】:2014-07-10 13:57:33
【问题描述】:

我目前有一种方法可以通过将 XML 文件与 XSD 文件进行比较来验证它。

private boolean validateXmlFile() {
    try {
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema;

        schema = factory.newSchema(new File("school.xsd"));

        Validator validator = schema.newValidator();
        Source source = new StreamSource(new File("school.xml"));

        validator.validate(source);

        return true;
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

最初,XSD 文件位于主目录中,但现在我正在尝试将我的项目转换为“mavenised”,这涉及将我的所有文件放在“resources/”目录中。

既然 XSD 文件位于资源目​​录中,我如何调用它的路径名?显然我不能这样做:

schema = factory.newSchema(new File("resources/school.xsd"));

是否涉及将文件称为 InputStream?

非常感谢

【问题讨论】:

  • this.class.getResourcesAsStream("/school.xsd");.

标签: java xml maven xsd


【解决方案1】:

这就是我一直在寻找的东西,无论如何谢谢:

schema = factory.newSchema(new File(this.getClass().getResource("school.xsd").toURI()));

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 2013-06-06
    • 1970-01-01
    • 2013-07-09
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多