简体   繁体   English

X从依赖java模块的资源中包含xml文件

[英]XInclude xml file from resources of dependent java module

I have a java application that contains of 2 parts: Core project(module) and App project(module). 我有一个包含2个部分的java应用程序:核心项目(模块)和应用程序项目(模块)。 Core module is dependent by several apps. 核心模块依赖于多个应用程序。 I configure the work of my App module in config.xml file. 我在config.xml文件中配置我的App模块的工作。 I want to put xml file with common settings to Core module's resources to let all apps use xinclude to include this part to their configs. 我想将带有常用设置的xml文件放到Core模块的资源中,让所有应用程序都使用xinclude将这部分包含在他们的配置中。 I use SAXParser to parse config.xml . 我使用SAXParser来解析config.xml This is what my config.xml looks like: 这就是我的config.xml的样子:

<?xml version="1.0" encoding="UTF-8" ?>
<myapp>
    <xi:include href="common.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
    <app-specific-data>
    ...
    </app-specific-data>
</myapp>

File common.xml is places in CoreModule/src/resources. 文件common.xml位于CoreModule / src / resources中。 How can I access this file from config.xml that is places in working directory on AppModule? 如何从AppModule上工作目录中的config.xml访问此文件? Thank you. 谢谢。

When you do 当你这样做

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setXIncludeAware(true);
factory.setNamespaceAware( true );
SAXParser parser = factory.newSAXParser();
parser.parse(input, new MyHandler());

in your MyHandler class override resolveEntity(publicId, systemId) method. 在您的MyHandler类中重写resolveEntity(publicId,systemId)方法。 In config change "common.xml" to "cp://common.xml". 在config中将“common.xml”更改为“cp://common.xml”。 It will let you get "cp://common.xml" as systemId parameter in resolveEntity() method. 它将允许您在resolveEntity()方法中将 “cp://common.xml”作为systemId参数。

<xi:include href="cp://common.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>

In resolveEntity() method do next: resolveEntity()方法中执行下一步:

if( systemId.startsWith( "cp://" )  ){
    InputStream is = this.getClass().getClassLoader().getResourceAsStream( systemId.substring( 5 ) );
    InputSource isource = new InputSource( is );
    isource.setPublicId( publicId );
    isource.setSystemId( systemId );
    return isource;
} else {
    return super.resolveEntity( publicId, systemId );
}

If you omit 如果省略

isource.setPublicId( publicId );
isource.setSystemId( systemId );

you'll get NPE in searchforrecursiveincludes() in XIncludeHandler . 您将在XIncludeHandler中的searchforrecursiveincludes()中获得NPE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM