package com.zcd.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class TestSax {
private TestSax() {
}
private static TestSax instance = new TestSax();
private static Hashtable hashtable = new Hashtable();
public static TestSax getInstance() {
return instance;
}
public String getValueBykey(String key) {
if (hashtable == null || hashtable.size() <= 0) {
init();
}
return (String) hashtable.get(key);
}
private void init() {
Element e = getElement();
if (e != null) {
getValueToHashTable(e);
}
}
private void getValueToHashTable(Element e) {
List children = e.getChildren();
if (children != null && children.size() > 0) {
for (Iterator iterator = children.iterator(); iterator.hasNext();) {
Object object = iterator.next();
Element element = (Element) object;
if (element != null) {
String secondname = element.getName();
// System.out.println("secondname="+secondname);
List secondeChildren = element.getChildren();
if (secondeChildren != null && secondeChildren.size() > 0) {
Iterator isIterator = secondeChildren.iterator();
while (isIterator.hasNext()) {
Object second = isIterator.next();
Element third = (Element) second;
if (third != null) {
String thirdname = third.getName();
String name = (new StringBuilder())
.append(secondname).append(".")
.append(thirdname).toString();
String value = third.getValue();
hashtable.put(name, value);
}
}
}
}
}
}
}
private Element getElement() {
SAXBuilder builder = new SAXBuilder(false);
InputStream iStream = getClass().getClassLoader().getResourceAsStream(
"applicationConfig.config");//
try {
Document document = builder.build(iStream);
Element root = document.getRootElement();
return root;
} catch (JDOMException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println(TestSax.getInstance().getValueBykey(
"menuInfo.operCode"));
}
}
===================================================================
说明:
class.getClassLoader().getResourceAsStream($path), 其中$path默认是src的路径, 如果需要获取对应包下的文件, e.g: com.apache.http下的config.properties, 则class.getClassLoader().getResourceAsStream("com/apache/http/config.properties")
附:applicationConfig.config
::::::::
<?xml version="1.0" encoding="utf-8"?>
<applicationConfig>
<ria_auth>
<auth_SystemNo>SYS108</auth_SystemNo>
<auth_ResTypeCode>ALL_PAGECONTROL</auth_ResTypeCode>
<auth_RuleCode>PAGECONTROL_RULE_001</auth_RuleCode>
<auth_opr_write>PAGECONTROL_OPER_001</auth_opr_write>
<auth_opr_readOnly>PAGECONTROL_OPER_002</auth_opr_readOnly>
<auth_opr_invalidation>PAGECONTROL_OPER_003</auth_opr_invalidation>
<auth_opr_hidden>PAGECONTROL_OPER_004</auth_opr_hidden>
</ria_auth>
<riamulitlang>
<isdebug>true</isdebug>
</riamulitlang>
<right>
<isRight>false</isRight>
<noAuthorizationPage></noAuthorizationPage>
</right>
<systemInfo>
<ruleCode>SYSTEM_RULE_001</ruleCode>
<operCode>SYSTEM_OPER_001</operCode>
<enterpriseNo>00</enterpriseNo>
<resTypeCode>ALL_SYSTEM</resTypeCode>
<systemNo>SYS10000</systemNo>
<propCode>RES</propCode>
<typePropDetailOrgId></typePropDetailOrgId>
<typePropDetailResCode></typePropDetailResCode>
<typePropDetailStateCode></typePropDetailStateCode>
<propExtendVal></propExtendVal>
<!--点击率统计时间粒度,单位秒-->
<pageHitTime>60</pageHitTime>
</systemInfo>
<menuInfo>
<ruleCode>MENU_DATA_001</ruleCode>
<operCode>MENU_OPER_001</operCode>
<enterpriseNo>00</enterpriseNo>
<resTypeCode>ALL_MENU</resTypeCode>
<propCode>RES</propCode>
<systemNo>SYS10000</systemNo>
<typePropDetailOrgId></typePropDetailOrgId>
<typePropDetailResCode></typePropDetailResCode>
<typePropDetailStateCode></typePropDetailStateCode>
<propExtendVal></propExtendVal>
<menuDisPlay>false</menuDisPlay>
</menuInfo>
<toolBarInfo>
<ruleCode>TOOLBAR_DATA_001</ruleCode>
<operCode>TOOLBAR_OPER_001</operCode>
<enterpriseNo>00</enterpriseNo>
<resTypeCode>ALL_TOOLBAR</resTypeCode>
<propCode>RES</propCode>
<systemNo>SYS10000</systemNo>
<typePropDetailOrgId></typePropDetailOrgId>
<typePropDetailResCode></typePropDetailResCode>
<typePropDetailStateCode></typePropDetailStateCode>
<propExtendVal></propExtendVal>
<toolBarDisPlay>true</toolBarDisPlay>
</toolBarInfo>
<sessioninfo>
<menuId>sessionmenuid</menuId>
<pageCode>sessionpagecode</pageCode>
<memoryUrl>memoryurl</memoryUrl>
<userid>userid</userid>
<username>username</username>
<OrgId>OrgId</OrgId>
<loginInfoKey>JamLoginInfo</loginInfoKey>
<userId>mUserId</userId>
<userName>mUserName</userName>
<userDepartmentNo>mUserDepartmentNo</userDepartmentNo>
<currentEnterpriseNo>mCurrentEnterpriseNo</currentEnterpriseNo>
<currentSystemNo>SYS10000</currentSystemNo>
</sessioninfo>
<jamGenral>
<resInheridFlag>false</resInheridFlag>
<orgInheridFlag>false</orgInheridFlag>
<converseRightFlag>false</converseRightFlag>
</jamGenral>
<wsmpGenral>
<serviceKey>AppStore</serviceKey>
</wsmpGenral>
<misp>
<dcacheFlag>false</dcacheFlag><!--Dcache开关-->
<dcacheSpace>0</dcacheSpace> <!--Dcache空间可配置-->
<dcacheSpace2>1</dcacheSpace2> <!--Dcache空间可配置-->
<excuteSqlWarnTime>1000</excuteSqlWarnTime><!--sql执行预警时间阀值,单位毫秒,-1为关闭预警-->
<excuteSqlDegradeTime>1000</excuteSqlDegradeTime><!--sql执行降级时间阀值,单位毫秒,-1为关闭降级功能-->
<sysconfig>true</sysconfig><!-- msys_config表主键查询方式,false走原来查询方式,true走缓存解析方式 -->
</misp>
<logConfig>
<log2FileLocal>zh</log2FileLocal>
<i18nResource>log_applicaiton</i18nResource>
</logConfig>
<exceptionConfig>
<i18nResource>exception_applicaiton</i18nResource>
<exceptionFileName>/exception-info.xml</exceptionFileName>
</exceptionConfig>
<loginInfo>
<loginPage>/uiloader/forward.html</loginPage>
<authoriPage>/jam/common/noPrivilege.jsp</authoriPage>
<isHttpsLogin>false</isHttpsLogin>
</loginInfo>
<upload>
<maxUploadSize>102400</maxUploadSize>
<maxMemorySize>10240</maxMemorySize>
<tempDir>e:\</tempDir>
</upload>
<timer>
<log_path>/home/zxin10/was/tomcat/logs/timer_log.txt</log_path>
</timer>
<appLogPolicy>
<enabled>Y</enabled>
<handletype>2</handletype>
<remaindays>3</remaindays>
</appLogPolicy>
<omm_task>
<!--开关,0关,1开,默认1 -->
<switch>0</switch>
<!--数据库连接池名称,与config.xml name一致,缺省133 -->
<dbpoolname>133</dbpoolname>
<!--访问特定的网页-->
<linkPage>http://store.wo.com.cn/appstore_agent/return.do</linkPage>
<time_total>6000</time_total>
</omm_task>
<store>
<checkprice>true</checkprice>
<dcacheflag>true</dcacheflag><!-- dcache是否开启开关 -->
<dcache_time_level1>43200</dcache_time_level1><!-- 缓存清除时间设置:30分钟 -->
<dcache_time_level2>43200</dcache_time_level2><!-- 缓存清除时间设置:2小时 -->
<dcache_time_level3>43200</dcache_time_level3><!-- 缓存清除时间设置:12小时 -->
<dcache_time_level4>43200</dcache_time_level4><!-- 缓存清除时间设置:24小时 -->
<dcache_time_level5>0</dcache_time_level5><!-- 缓存清除时间设置:不过期 -->
<dcache_time_level7>2592000</dcache_time_level7>
<verifycode_dcacheSpace>0</verifycode_dcacheSpace> <!--验证码Dcache空间可配置-->
<dcache_AppRecom_time_level>300</dcache_AppRecom_time_level><!--验证码Dcache缓存时间-->
<dcacheSpace>2</dcacheSpace>
<dcache_Usercode_time_level>300</dcache_Usercode_time_level><!--验证码Dcache缓存时间-->
<dcacheSpace2>2</dcacheSpace2>
<dcacheUserSpace>2</dcacheUserSpace>
</store>
<check>
<key>shFLOW^(</key>
<weixinkey>180XRP58M0007Q9TE09N973DR1088HT0</weixinkey>
<twokey>X81XRP58M0007Q9TE09N973DR1088HT0</twokey>
<appHttpKey>abcdef</appHttpKey><!--3des加密密钥-->
<phonesegment>1</phonesegment><!-- 定向流量套餐号段开关,0关,1开,默认1 -->
</check>
<vacservice>
<vacserviceurl>http://10.129.170.114:9004/wsdlProject/services/PortalEngine</vacserviceurl>
<srcDeviceType>1000</srcDeviceType>
<srcDeviceID>200201</srcDeviceID>
<userIDType>0</userIDType>
<serviceType></serviceType>
<spAreaId></spAreaId>
</vacservice>
<advFtp><!--批量订购接口参数,读取以及上传批量订购文件用-->
<ip>211.95.66.31</ip>
<port>21</port>
<user>pkflow</user>
<pwd>pkFLOW@$</pwd>
<dir>/home/pkflow/data/</dir>
<dirbak>/home/pkflow/data/databak</dirbak>
</advFtp>
<advFtpcenter><!--批量订购接口参数,读取以及上传批量订购文件用-->
<ip>10.46.178.35</ip>
<port>21</port>
<user>root</user>
<pwd>App@zte0</pwd>
<dir>/home/stlcenter/data/</dir>
<dir1>/home/stlcenter/cp/</dir1>
</advFtpcenter>
<unorderdir>
<dir>/home/zxin10/was/tomcat/logs/unorder/</dir>
</unorderdir>
<flowplatform><!--北分系统客户端参数-->
<urlorder>http://61.50.254.204:8088/vip/order/</urlorder><!--北分订购接口-->
<urlunorder>http://61.50.254.204:8088/vip/unsubscribe/</urlunorder><!--北分退订接口-->
<findorder>http://61.50.254.204:8088/vip/infoserch/</findorder><!--北分查询接口-->
<secret>676212</secret><!--密钥-->
<wostorechannelid>71671</wostorechannelid><!--渠道商ID-->
</flowplatform>
<userlevel><!--北分系统客户端参数-->
<diamond>1024</diamond>
<gold>500</gold>
<silver>300</silver>
<copper>50</copper>
</userlevel>
<sdkrequest>
<url>http://druid.punchbox.info/telecom/notice_gate.php</url>
<secret>shFLOW^(</secret>
</sdkrequest>
<allofpack>
<cpid>91639</cpid>
</allofpack>
<dm>
<url>http://140.206.176.11:18430/openwostore/dm/getfivefiled</url>
<appkey>9B3573C456F6AAE90C26D45CAE1B4D59</appkey>
</dm>
<nextmonth>
<province>新疆,海南,西藏,云南,广东,青海,内蒙古,北京,河北,河南,黑龙江,辽宁,山东,上海,宁夏,湖北,天津</province>
</nextmonth>
<yumai>
<province>福建,陕西,四川,广西,江西,湖北,吉林,宁夏,天津</province>
</yumai>
<threemonthfree>
<!--<province>四川,浙江,河南,福建,湖北</province>-->
<province>四川,浙江,河南,福建,湖北,山西,江西,宁夏,辽宁,内蒙古,海南,天津</province>
</threemonthfree>
<Brand_name>
<sign>276A81949AE5DCB072CD3ED8601344E9</sign>
<url>http://140.206.176.11:18430/openwostore/userinfo/getOnlyUserInfo/</url>
</Brand_name>
<sendSMS>
<smsUrl>http://passport.wostore.cn:8090/smsProxy/soo/sms/send</smsUrl>
<client_id>USER</client_id>
<client_secret>5d64e460b5bd8522</client_secret>
</sendSMS>
</applicationConfig>