java生成xml

import java.io.IOException;
import java.io.StringWriter;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class MyTestOne {

/**
* @param args
*/
public String createXml() {
// TODO Auto-generated method stub
Element root = new Element("root");
//根据root节点生成document
Document doc = new Document(root);
//root下添加header节点
Element head = new Element("header");
root.addContent(head);
//root下添加body节点
Element body = new Element("body");
Element name = new Element("name");
name.addContent("kvone");
body.addContent(name);
root.addContent(body);

XMLOutputter out = new XMLOutputter();
StringWriter strOut = new StringWriter();
try {
out.output(doc, strOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return strOut.toString();
}

}

你可能感兴趣的:(java,xml)