xi:包含在jar文件中的xml文件中不能在WildFly中工作

时间:2017-04-13 11:34:36

标签: java xml module wildfly xerces

以下是该方案:

我捆绑了应用程序在.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

我尝试过(&amp; error&#39; d)所有可以考虑的选项。 xi:include .xml文件在安装为WildFly模块的jar文件中的正确组合是什么?

编辑:这里是如何加载xmlfile的:

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)。

2 个答案:

答案 0 :(得分:2)

这取决于您的XML解析器。

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

上述文档使用 SAX API EntityResolver界面来查找XML文档中的资源。

答案 1 :(得分:1)

删除&#34;资源:&#34;来自您的URL的前缀,因为这似乎是JBoss特定的协议。

同时发出&#34;资源/&#34; path,如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>