基于DOM4J
package com.xiaoming;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class MyTest {
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
root.addElement("code").addText("1");
root.addElement("msg").addText("好消息");
System.out.println("前:" + doc.asXML());
doc.getRootElement().node(0).setText("0");
doc.getRootElement().node(1).setText("什么好消息啊");
System.out.println("后: " + doc.asXML());
}
}
控制台:
前:<?xml version="1.0" encoding="UTF-8"?>
<root><code>1</code><msg>好消息</msg></root>
后: <?xml version="1.0" encoding="UTF-8"?>
<root><code>0</code><msg>什么好消息啊</msg></root>