引入JDOM包
package xml;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class JdomXmlTest {
public static void main(String[] args) {
Element rootElement = new Element("user");
Document myDocument = new Document(rootElement);
Element items = new Element("items");
Element item = new Element("item");
item.addContent(new Element("id").addContent("11"));
item.addContent(new Element("name").addContent("jessen"));
item.addContent(new Element("sex").addContent("male"));
items.addContent(item);
rootElement.addContent(items);
XMLOutputter utputter = new XMLOutputter();
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
try {
outputter.output(myDocument, byteRep);
System.out.println(byteRep.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}