需要用到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
}

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49
