【问题标题】:Java SAXParser cannot read imported XML files in resource directoryJava SAXParser 无法读取资源目录中导入的 XML 文件
【发布时间】:2015-12-03 06:48:15
【问题描述】:

谁能帮忙

我在名为 GML-XSD 的目录中有一个 XSD 文件,并使用 Netbeans 8.0.2,在源代码中我对这个文件使用了一个字符串

String gmlXSDFile = "etc/GML_XSD/epsg/EPSG.xsd";

目录为project/GML-XSD/epsg/EPSG.xsd 问题是当我构建项目并在 Glassfish Server 或 Jetty 上运行它时,它抱怨找不到 etc 文件夹。我该如何解决这个问题,以确保它会尝试在 war 文件中找到“etc/GML_XSD”,而不是在部署它的目录中找到。

谢谢。

 Failed to read schema document 'file:/usr/local/ras/log/etc/GML_XSD/epsg/EPSG.xsd', because 1) could not find the document

编辑:读取此文件的代码(无效)

Source[] sources = new StreamSource[1];
sources[0] = new StreamSource(new File(gmlXSDFile));

编辑:图像内的新代码

 String gmlXSDFile = "/GML/epsg/EPSG.xsd";

可以支持正确读取EPSG.xsd文件,但不能导入属于下面其他包的其他xsd文件:

  <import namespace="http://www.opengis.net/gml/3.2" schemaLocation="../gml/3.2.1/gml-3.2.1.xsd"/>

无法将名称“gml:Definition”解析为 (n)“元素声明”组件

【问题讨论】:

  • 提供您如何读取或访问文件的代码
  • 我已经从一个新文件中编辑了这个读取。
  • 错误似乎是 xml 验证而不是加载资源失败。您是否有此异常的完整堆栈跟踪。
  • 实际上它来自“导入的 XSD”,因为如果我从 getResourceAsStream 更改为 new StreamSource(new File("GML/epsg/EPSG.xsd"));然后它会工作。

标签: java xml string xml-parsing saxparser


【解决方案1】:

我尝试了很多方法都无法工作,最终我成功了

  URL schemaURL = Thread.currentThread().getContextClassLoader().getResource("GML/epsg/EPSG.xsd");


  Schema schema = factory.newSchema(schemaURL);

并且不需要创建任何无济于事的包或资源包。我现在很开心。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多