如何在 Web 中讀取 xml 配置文件的配置信息
我的配置文件config.xml:
?????????????
<
config
>
???
?????????????
<
java
>
c:\jdk
</
java
>
</
config
>
工程的結構如下圖:
Config.xml
文件在src目錄下,web工程發布后,config.xml文件存放在WEB-INF/classes/目錄下。
在action中,來取得xml的屬性配置文件,代碼如下:
???????????????? Document? doc=
XMLReader.readXML("config.xml")
;
???????????????? System.
out
.println(
"action? document? is? :? "
+doc);????
????????????????
Node jhn=(Node)doc.selectSingleNode("/config/java[1]");
???????????????? System.
out
.
println
(
"java home? node? is? :? "
+jhn);
???????????????? System. out .println( "the value of the java node? is? :? " +jhn.getStringValue());
程序的執行結果如下:
在取得節點的值的時候,用的是xpath。
????
(Node)doc.selectSingleNode("/config/java[1]")
中
/config/java[1]
表示取
config
節點下的第一個
java
節點,結果以
Node
類型的對象返回。
jhn.getStringValue()
來取得此節點的值。
想了解更多的
xpath
的知識,可以參考
http://www.aygfsteel.com/zhyiwww/archive/
?
|----------------------------------------------------------------------------------------|
版權聲明 版權所有 @zhyiwww
引用請注明來源 http://www.aygfsteel.com/zhyiwww
|----------------------------------------------------------------------------------------|