2017-04-13 91 views
7

不起作用这里的情景:十一:包括JAR文件中的XML文件中WildFly

我捆绑几个.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文件的正确组合是什么?

编辑:这里的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)。

+0

什么是你的使用情况?真的有必要将您的设置打包到jar文件中吗? – 2017-04-21 20:57:39

+0

http://stackoverflow.com/questions/8412798/how-to-load-xmlcatalog-from-classpath-resources-inside-a-jar-reliably 看看是否有帮助 – 2017-04-22 06:10:51

+1

你可以显示你用于创建的代码解析器/映射器? – ctomc

回答

1

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

还会发出“资源/”路径,因为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> 
+0

什么是流的“相对路径”?并非所有的XML文档都来自真正的文件系统... –

+0

相对路径是例如在上面的文件列表中的“资源”。如果您设法获得1.xml加载,2.xml和3.xml必须相对于1.xml - >没有任何文件夹(当遵循问题中给出的结构时) –

+0

XML解析器不知道这一点。它会得到一个'Document',或者一个字符流或者其他可以读取字符的来源。 –