通过Digester动态读取xml配置文件
核心代码:
//InspurGISconfig.xml 配置文件 ,我们的Digester最后就是要把这个文件的信息变成json 对象形式,这里定义三个类,remoteService,searchLayers,insertLayers ,每个类中都有自己的属性
<?xml version="1.0" encoding="UTF-8"?>
<InspurGISConfig defaultMapId="1">
<remoteServices>
<RemoteService id="mapservice1" serverAddress="" serverPort="" mapServiceName="" mapName="" dataServiceName="" dataSourceName="">
</RemoteService>
</remoteServices>
<searchLayers>
<SearchLayer id="searchlayer1" searchLayerName="" returnValue="" limit="">
</SearchLayer>
</searchLayers>
<insertLayers>
<InsertLayer id="insertLayer1" insertLayerName="" insertValue="">
</InsertLayer>
</insertLayers>
</InspurGISConfig>
//这个是 解析上面config的rule文件
<?xml version="1.0" encoding="UTF-8"?>
<digester-rules>
<pattern value="InspurGISConfig">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.InspurGISConfiguration"/> //对应在文件中的javabean实体类
<set-properties-rule>
<alias attr-name="*" prop-name="*"/> //所有的属性信息都加载
</set-properties-rule>
<pattern value="remoteServices/RemoteService">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.RemoteService"/>
<set-next-rule methodname="addRemoteService"/>
<set-properties-rule>
<alias attr-name="*" prop-name="*"/>
</set-properties-rule>
</pattern>
<pattern value="searchLayers/SearchLayer">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.SearchLayer"/>
<set-next-rule methodname="addSearchLayer"/>
<set-properties-rule>
<alias attr-name="*" prop-name="*"/>
</set-properties-rule>
</pattern>
<pattern value="insertLayers/InsertLayer">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.InsertLayer"/>
<set-next-rule methodname="addInsertLayer"/>
<set-properties-rule>
<alias attr-name="*" prop-name="*"/>
</set-properties-rule>
</pattern>
</pattern>
</digester-rules>
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////通过Digester进行转换
URL ruleURL = Thread.currentThread().getContextClassLoader().getResource("rules/InspurGISConfigRule.xml");
Digester dis = DigesterLoader.createDigester(ruleURL);
dis.setUseContextClassLoader(true);
InspurGISConfiguration inspurGISConfig = null;
try {
InputStream filePath = new FileInputStream(xmlparse); //这里的xmlparse就是config的路径
inspurGISConfig = (InspurGISConfiguration)dis.parse(filePath); //转换之后为一个InspurGISconfiguration 的一个java实例
}
//InspurGISconfig.xml 配置文件 ,我们的Digester最后就是要把这个文件的信息变成json 对象形式,这里定义三个类,remoteService,searchLayers,insertLayers ,每个类中都有自己的属性
<?xml version="1.0" encoding="UTF-8"?>
<InspurGISConfig defaultMapId="1">
<remoteServices>
<RemoteService id="mapservice1" serverAddress="" serverPort="" mapServiceName="" mapName="" dataServiceName="" dataSourceName="">
</RemoteService>
</remoteServices>
<searchLayers>
<SearchLayer id="searchlayer1" searchLayerName="" returnValue="" limit="">
</SearchLayer>
</searchLayers>
<insertLayers>
<InsertLayer id="insertLayer1" insertLayerName="" insertValue="">
</InsertLayer>
</insertLayers>
</InspurGISConfig>
//这个是 解析上面config的rule文件
<?xml version="1.0" encoding="UTF-8"?>
<digester-rules>
<pattern value="InspurGISConfig">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.InspurGISConfiguration"/> //对应在文件中的javabean实体类
<set-properties-rule>
<alias attr-name="*" prop-name="*"/> //所有的属性信息都加载
</set-properties-rule>
<pattern value="remoteServices/RemoteService">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.RemoteService"/>
<set-next-rule methodname="addRemoteService"/>
<set-properties-rule>
<alias attr-name="*" prop-name="*"/>
</set-properties-rule>
</pattern>
<pattern value="searchLayers/SearchLayer">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.SearchLayer"/>
<set-next-rule methodname="addSearchLayer"/>
<set-properties-rule>
<alias attr-name="*" prop-name="*"/>
</set-properties-rule>
</pattern>
<pattern value="insertLayers/InsertLayer">
<object-create-rule classname="com.inspur.mygis.xmlParse.XMLBeans.InspurGISConfig.InsertLayer"/>
<set-next-rule methodname="addInsertLayer"/>
<set-properties-rule>
<alias attr-name="*" prop-name="*"/>
</set-properties-rule>
</pattern>
</pattern>
</digester-rules>
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////通过Digester进行转换
URL ruleURL = Thread.currentThread().getContextClassLoader().getResource("rules/InspurGISConfigRule.xml");
Digester dis = DigesterLoader.createDigester(ruleURL);
dis.setUseContextClassLoader(true);
InspurGISConfiguration inspurGISConfig = null;
try {
InputStream filePath = new FileInputStream(xmlparse); //这里的xmlparse就是config的路径
inspurGISConfig = (InspurGISConfiguration)dis.parse(filePath); //转换之后为一个InspurGISconfiguration 的一个java实例
}