81,092
社区成员
发帖
与我相关
我的任务
分享
package XML;
import java.io.File;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public class Config {
private static XMLConfiguration XML;
//初始化
public Config(String File) throws Exception{
try{
XML.setFile(new File(new File(File).getAbsolutePath()));
XML.load();
}catch(ConfigurationException e){
e.printStackTrace();
}
}
//增加节点
public boolean addKey(String key,String value){
try{
XML.addProperty(key,value);
return true;
}catch(Exception e){
return false;
}
}
//对节点赋值
public boolean setKey(String key,String value){
try{
XML.setProperty(key,value);
return true;
}catch(Exception e){
return false;
}
}
//保存XML文件
public boolean Save(){
try{
XML.save();
return true;
}catch(Exception e){
return false;
}
}
//获取节点值
public String getKey(String key) throws Exception{
return XML.getString(key);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<config>
<test>Hello XML</test>
</config>
</root>
<%@page contentType="text/html; charset=GBK"%>
<%@page import="XML.Config"%>
<%
Config XML=new Config("C:/file.xml");
out.println(XML.getKey("config.test"));
%>