posts - 0,  comments - 5,  trackbacks - 0

          需要用到commons-logging-1.0.4.jar,dom4j-1.4.jar包

          ?1 import?java.io.File;
          ?2 import?java.io.IOException;
          ?3
          ?4 import?org.apache.commons.logging.Log;
          ?5 import?org.apache.commons.logging.LogFactory;
          ?6 import?org.dom4j.Document;
          ?7 import?org.dom4j.io.SAXReader;
          ?8
          ?9 /**
          10 ?*?@author?wufalong
          11 ?*
          12 ?*?TODO?To?change?the?template?for?this?generated?type?comment?go?to
          13 ?*?Window?-?Preferences?-?Java?-?Code?Style?-?Code?Templates
          14 ?*/
          15 public?class?LoadRootElementByConfigFile?{
          16 ????private?static?Log?log?=?LogFactory.getLog(LoadRootElementByConfigFile.class);
          17 ????public?static?Document?getRootElement(){
          18 ????????String?path="";
          19 ????????File?file?=?new?File(".");
          20 ????????try
          21 ????????{
          22 ????????????path?=?file.getCanonicalPath();
          23 ????????}
          24 ????????catch?(IOException?e)
          25 ????????{
          26 ????????????e.printStackTrace();
          27 ????????}
          28
          29 ????????Document?document?=?null;
          30 ????????String?filePath?=?"";
          31 ????????try{?
          32 ????????????filePath?=?path+"/config.xml";
          33 ????????????File?f?=?new?File(filePath);??//服務器運行目錄
          34 ????????????SAXReader?reader?=?new?SAXReader();
          35 ????????????document?=?reader.read(f);
          36 ????????}catch(Exception?e){?
          37 ????????????filePath?=?path+"/src/config.xml";
          38 ????????????File?f?=?new?File(filePath);?//eclipse調試目錄
          39 ????????????SAXReader?reader?=?new?SAXReader();
          40 ????????????try{
          41 ????????????????document?=?reader.read(f);
          42 ????????????}catch(Exception?ex){
          43 ????????????????
          44 ????????????}
          45 ????????}
          46 ????????log.info("配置文件路徑:"+filePath);
          47 ????????return?document;????
          48 ????}
          49 }
          posted on 2007-09-03 10:59 crazy 閱讀(258) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 南通市| 万载县| 元朗区| 宜昌市| 卓尼县| 卢湾区| 东乡族自治县| 逊克县| 宜君县| 平湖市| 安徽省| 阿坝| 武城县| 洪雅县| 尚义县| 平湖市| 获嘉县| 通渭县| 台北县| 海门市| 土默特左旗| 淄博市| 邵阳县| 垫江县| 灵川县| 阳江市| 梁山县| 儋州市| 漳州市| 盘锦市| 湘西| 丹巴县| 湘乡市| 阿拉善左旗| 景宁| 苍山县| 凤凰县| 龙江县| 东莞市| 瓮安县| 桦南县|