你可以使用JDOM包解决这个问题,我这里有详细的源代码,你给我E-mail我发给你,是自己写的,很简单一看就应该明白。
解决方案 »
- ActiveMQ EOFException ,急需解决!
- struts2过滤器都做了什么
- 如何获得跳转过来的前一页URL
- 新手提问 spring 集合注入问题
- webwork 的iterator同时读多条记录
- 如何用jmail 发送html各式的邮件?
- 请教:处理Servlet请求信息,出现的问题
- jboss重启报端口被占用的问题(在线等....)
- 求在WSAD5.0上已运行通过的EJB的例子?
- servlet设置cookie,jsp获得cookie,为什么获得2个cookie?但是在浏览器目录下,只有一个cookie文件,求助!!!
- 请问在EJB中,怎样同过JB7在XML的配置描述器中加入些特定的环境属性??谢谢
- 在线等待:进来看看这个错误
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class XMLProperties
{
private boolean autoSave = true;
private File file;
private Document doc;
private Map propertyCache = new HashMap(); public XMLProperties(String file, boolean autoSave, boolean validate)
throws JDOMException
{
this.autoSave = autoSave;
this.file = new File(file);
SAXBuilder builder = new SAXBuilder(validate);
try
{
doc = builder.build(file);
}
catch (JDOMException ex)
{
throw new JDOMException("构造过程出现错误! " + ex);
}
} public XMLProperties(String file, boolean autoSave) throws JDOMException
{
this(file, autoSave, false);
} public XMLProperties(String file) throws JDOMException
{
this(file, false, false);
}
public boolean isAutoSave() { return autoSave; } public void setAutoSave(boolean autoSave) { this.autoSave = autoSave; } public synchronized void saveProperties() throws IOException
{
if(autoSave == false) return; XMLOutputter out = new XMLOutputter(" ",true, "GB2312");
File temp = null;
FileWriter writer = null;
boolean isError = false;
try
{
//先生成一个临时文件
temp = new File(file.getParent(), file.getName() + ".tmp");
writer = new FileWriter(temp);
out.output(doc, writer);
}
catch (IOException ex)
{
isError = true;
throw new IOException("生成临时文件出现错误!" + ex);
}
finally
{
writer.close();
}
//备份源文件,生成新文件
if(isError == false)
{
File bak = new File(file.getParent(), file.getName() + ".bak");
//删除原来的备份文件
if(bak.exists()) bak.delete();
boolean isSuccess = file.renameTo(bak);
file.delete();
//临时文件变成正式文件
isSuccess = temp.renameTo(file);
if(isSuccess == false)
throw new IOException("备份文件过程出现错误!");
}
} public String getPorperty(String name)
{
if(propertyCache.containsKey(name))
return propertyCache.get(name).toString(); //查找指定的元素元素
Element element = this.findOnly(name);
if(element == null) return null; String value = element.getTextTrim();
propertyCache.put(name, value);
return value;
} public String getPorperty(String name, String attr)
{
String nameAttr = name + ":" + attr;
if(propertyCache.containsKey(nameAttr))
return propertyCache.get(nameAttr).toString(); //查找指定的元素元素
Element element = this.findOnly(name);
if(element == null) return null; String value = element.getAttributeValue(attr);
propertyCache.put(nameAttr, value);
return value;
} public void setProperty(String name, String value) throws IOException
{
propertyCache.put(name, value); //查找指定的元素元素
Element element = this.findCreate(name);
element.setText(value);
saveProperties();
}
public void setProperty(String name, String attr, String value) throws IOException
{
String nameAttr = name + ":" + attr;
propertyCache.put(nameAttr, value); //查找指定的元素元素
Element element = this.findCreate(name);
element.setAttribute(attr, value);
saveProperties();
} public void deleteProperty(String name) throws IOException
{
if(propertyCache.containsKey(name))
propertyCache.remove(name); Element element = this.findOnly(name);
if(element != null) element.detach();
saveProperties();
} public void deleteProperty(String name, String attr) throws IOException
{
String nameAttr = name + ":" + attr;
if(propertyCache.containsKey(nameAttr))
propertyCache.remove(nameAttr); Element element = this.findOnly(name);
if(element != null) element.removeAttribute(attr);
saveProperties();
} public String[] getChildrenProperties(String parent)
{
//分解元素的名称
String[] propName = parsePropertyName(parent);
Element element = doc.getRootElement();
//遍历搜索匹配的元素
for (int i = 0; i < propName.length; i++)
{
element = element.getChild(propName[i]);
//没有匹配的元素,返回一个空的数组
if(element == null) return new String[]{};
}
//找到啦!
List children = element.getChildren();
String[] childrenName = new String[children.size()];
for (int i = 0; i < children.size(); i++)
{
childrenName[i] = ((Element)children.get(i)).getName();
}
return childrenName;
} private String[] parsePropertyName(String name)
{
//因为符号"."是保留字符,所以不能使用String.split()
//所以先进行一次替代
return name.replace('.','#').split("#");
}
private Element findOnly(String name)
{
//分解元素的名称
String[] propName = parsePropertyName(name);
Element element = this.doc.getRootElement();
//遍历搜索匹配的元素
for (int i = 0; i < propName.length; i++)
{
element = element.getChild(propName[i]);
if(element == null) return null;
}
//找到啦!
return element;
} private Element findCreate(String name)
{
//分解元素的名称
String[] propName = parsePropertyName(name);
Element element = doc.getRootElement();
//遍历搜索匹配的元素,不存在则创建它
for (int i = 0; i < propName.length; i++)
{
if(element.getChild(propName[i]) == null)
{
//自动创建该元素
element.addContent(new Element(propName[i]));
}
element = element.getChild(propName[i]);
}
//找到啦!
return element;
}
}