2017-04-13 100 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',或者一個字符流或者其他可以讀取字符的來源。 –