日期:2014-05-18 浏览次数:20742 次
public static void update() {
String oldStr = "d:/a.xml";
String newStr = "d:/b.xml";
Document document = null;
try {
SAXReader saxReader = new SAXReader(); // 用来读取xml文档
document = saxReader.read(new File(oldStr)); // 读取xml文档
List list = document.selectNodes("list/name");// 用xpath查找节点book的内容
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
element.setText("张三");// 设置相应的内容
}
} catch (Exception e) {
e.printStackTrace();
}
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileWriter(new File(newStr)),format);
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}