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 阅读(239) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: