⊙﹏⊙b汗。。。。这个重写完以后,你要将写完后的值重新写到你的文件里面去的!
那块我没给你写。。。。。。。。
先==啊,一会给你写!~
代码中:XMLWriter output = new XMLWriter(new FileWriter(
new File("c:/catalog-modified.xml"))); //file换成你自己的xml文件
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class UpdateDom4j {
public static void updateXml(String xml) {
Document doc = null;
try {
doc = DocumentHelper.parseText(xml); //将xml生成Document
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List list = doc.selectNodes("/setting/person"); //查询指定标签,返回该标签的list
/*
* 解析list
* */
if(!list.isEmpty()) {
for(Element element : list) {
//获取指定标签属性的值
//解析标签间的text内容
System.out.println(element.elementText("name"));
if("language".equals(element.elementText("name"))) {
Element e1 = element.element("value");
e1.setText("en");
}
System.out.println(element.elementText("value"));
}
}
try {
XMLWriter output = new XMLWriter(new FileWriter(
new File("c:/catalog-modified.xml"))); //file换成你自己的xml文件
output.write(doc);
output.close();
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
String xml = "" +
"languagezh" +
"snsopen" +
"";
updateXml(xml);
}
}