当前位置: 技术问答>java相关
如何修改xml中的内容
来源: 互联网 发布时间:2015-11-20
本文导语: one d:/xml two e:/two ------------------------------------ 会有很多项,如何修改one, two, 并且可以增加删除 | package util.common; import java.io.*; import java.util.*; import org.jdom.*; import org.jdom.input.*;...
one
d:/xml
two
e:/two
------------------------------------
会有很多项,如何修改one, two,
并且可以增加删除
|
package util.common;
import java.io.*;
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
import java.io.*;
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