import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Writer {
public static void main(String[] args) throws IOException {
//创建xml文档
Document document = DocumentHelper.createDocument();
//文档添加根节点,只能添加一个,添加多个: Cannot add another element to this Document as it already has a root element of
Element root = document.addElement("mymvc");
for (int i = 0; i < 2; i++) {
//添加子节点
Element actions = root.addElement("actions");
Element listAction = actions.addElement("action");
//设置子节点属性
listAction.addAttribute("name","list");
listAction.addAttribute("class","controller");
//设置子节点内容
listAction.setText("第"+(i+1)+"个hello,action!");
}
//创建输出格式
OutputFormat format = OutputFormat.createPrettyPrint();
//XML写入工具
XMLWriter writer=new XMLWriter(new FileWriter("ghy.xml"),format);
//写入文档至XML文件
writer.write(document);
//关闭流
writer.close();
}
}