怎样修改xml文件的属性值?

renrzg 2002-12-16 04:13:19
如:我现存一个xml文件test.xml
<?>
<properties>
<NAME>ZhangShan</NAME>
</properties>
我想将Name的值改为LiShi,然后再保存为a.xml文件?
...全文
291 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Patrick_DK 2002-12-16
  • 打赏
  • 举报
回复
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 < 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;
}
}
Alex_20 2002-12-16
  • 打赏
  • 举报
回复
你可以使用JDOM包解决这个问题,我这里有详细的源代码,你给我E-mail我发给你,是自己写的,很简单一看就应该明白。
ChinaOk 2002-12-16
  • 打赏
  • 举报
回复
Http://www.ChinaOK.net/Examples/

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧