SAX更改xml的内容
xml文件:
<?xml version= "1.0 " encoding= "gb2312 "?>
<messages>
<message id= "dirName ">
<title> dirName </title>
<content> jhgjg </content>
</message>
<message id= "toDirName ">
<title> fdsa </title>
<content> fasdf </content>
</message>
<message id= "tempDirName ">
<title> fdsa </title>
<content> fasdf </content>
</message>
</messages>
程序:
package danju;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
public class Config {
public static void main(String[] args) throws Exception {
SAXBuilder sb = new SAXBuilder();// 建立构造器
Document doc = sb.build(new FileInputStream( "config.xml "));// 读入指定文件
Element root = doc.getRootElement();// 获得根节点
List list = root.getChildren();// 将根节点下的所有子节点放入List中
new Config().setDirNameContent( "123 ");
for(int i=0;i <list.size();i++) {
System.out.println( "--------------------------- ");
Element item=(Element)list.get(i);//取得节点实例
String name=item.getAttribute( "id ").getValue();//取得属性值
System.out.println( "Name--> "+name);
Element sub=item.getChild( "title ");//取得当前节点的字节点
String text=sub.getText();//取得当前节点的值
System.out.println( "Title--> "+text);
Element sub2=item.getChild( "content ");
String text2=sub2.getText();
System.out.println( "Content--> "+text2);
}
}
}
怎么更改xml文件的内容,
如把
<message id= "toDirName ">
<title> fdsa </title>
<content> fasdf </content>
</message> 中的 <content> fasdf </content>
改为 <content> 123 </content>
------解决方案--------------------你会get、难道就不会set
然后输出到文件嘛
====================================
http://jysq.net/14906/viewspace_17106.html
给你个PPT参考!