使用dom4j创建xml
简单的代码如下:
public static void main(String[] args) { //web 开发中使用 dom4j构建xml返回报文,简单演示 org.dom4j.Document doc = DocumentHelper.createDocument(); //创建根节点 Element root = doc.addElement("response"); //创建根节点的子节点respcode Element respcode = root.addElement("respcode"); //创建 respcode 的子节点 respChild Element respChild = respcode.addElement("respChild1"); //设置respChild节点的文本节点值 respChild.setText("child1"); Element respChild2 = respcode.addElement("respChild2"); respChild2.setText("child2"); //将XML输出为字符串 //doc.asXML(); //输出的xml字符串为: //<?xml version="1.0" encoding="UTF-8"?> //<response><respcode><respChild1>child1</respChild1><respChild2>child2</respChild2></respcode></response> System.out.println(doc.asXML()); }