【问题标题】:xi:include in xml file within jar file does not work in WildFlyxi:包含在 jar 文件中的 xml 文件中在 WildFly 中不起作用
【发布时间】:2017-04-13 11:34:36
【问题描述】:

这是场景:

我捆绑了几个.xml(有点像配置)文件,以便我的应用程序在.jar 文件中运行。 jar文件结构如下:

settings-1.0.0.jar
˪ resources/
  ˪ 1.xml
  ˪ 2.xml
  ˪ 3.xml
˪ META-INF/
  ˪ MANIFEST.MF

1.xml有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xi="http://www.w3.org/2001/XInclude">
    <!-- Include 2 -->
    <xi:include
        href="resource:resources/2.xml" />
    <!-- Include 3 -->
    <xi:include
        href="resource:resources/3.xml" />
    <!--
    <map>
    </map>
    -->
</document>

基于this 文章。尝试访问这些包括时(成功部署我的应用程序后)我收到以下错误:

Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 43; An 'include' failed, and no 'fallback' element was found.
    at org.apache.xerces.parsers.DOMParser.parse(DOMParser.java:245)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:298)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
    at org.zcore.dkp.backend.mapper.MappingParser.parse(MappingParser.java:60)
    ... 327 more

我尝试了(&error'd)所有可以想到的选项。在作为 WildFly 模块安装的 jar 文件中,xi:include 的正确组合是什么?

编辑:下面是 xml 文件的加载方式:

private void loadBackendMapping() {
    try {
        BackendMappingParser parser = new BackendMappingParser();
        InputStream in = ResourceLoaderHelper.getResourceAsStream(BACKEND_MAPPING_RESOURCE);
        if (in == null) {
            throw new FileNotFoundException();
        }
        try {
            parser.parse(in);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                log.warn("Failed to close " + BACKEND_MAPPING_RESOURCE, e);
            }
        }
        backendMapping = parser.getMapping();
        if (log.isDebugEnabled()) {
            log.debug(BACKEND_MAPPING_RESOURCE + " successfully loaded!");
        }
    } catch (FileNotFoundException e) {
        log.warn("\"" + BACKEND_MAPPING_RESOURCE + "\" not found!");
        backendMapping = new BackendMapping();
    } catch (Exception e) {
        throw new CenterwareSystemException("Failed to parse " + BACKEND_MAPPING_RESOURCE, e);
    }
}

BACKEND_MAPPING_RESOURCE 包含文件名 (1.xml)。

【问题讨论】:

  • 您的用例是什么?真的有必要把你的设置打包成一个jar文件吗?
  • 你能展示你用来创建解析器/映射器的代码吗?
  • @ctomc 抱歉​​,没有注意到您的评论。 :-/ 添加了1.xml 是如何以编程方式加载的。

标签: java xml module wildfly xerces


【解决方案1】:

这取决于您的 XML 解析器。

根据您的例外情况,您似乎使用 SAX。
试试这篇文章: https://www.ibm.com/developerworks/library/x-tipentres/

上述文档使用 SAX APIEntityResolver 接口在 XML 文档中定位资源。

【讨论】:

    【解决方案2】:

    从您的 URL 中删除“resource:”前缀,因为这似乎是 JBoss 特定的协议。

    同时发出“resource/”路径,因为 1.xml、2.xml 和 3.xml 都位于同一个文件夹中,您通常指定相对路径(从 1 到 2 和从 1 到 3)。

    尝试以下方法:

    <?xml version="1.0" encoding="UTF-8"?>
    <document xmlns:xi="http://www.w3.org/2001/XInclude">
        <!-- Include 2 -->
        <xi:include href="2.xml" />
        <!-- Include 3 -->
        <xi:include href="3.xml" />
        <!--
        <map>
        </map>
        -->
    </document>
    

    【讨论】:

    • 什么是流的“相对路径”?并非所有 XML 文档都来自真实的文件系统...
    • 相对路径例如“资源”在顶部的文件列表中。如果您设法加载 1.xml,则 2.xml 和 3.xml 必须相对于 1.xml 进行寻址 -> 所以没有任何文件夹(当遵循问题中给出的结构时)
    • XML 解析器不知道这一点。它得到一个Document,或者一个字符流,或者它可以从中读取字符的其他来源。
    • 如果另外填写“SystemID”和“EntityResolver”,它可以自动找出相关资源的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2011-06-12
    相关资源
    最近更新 更多