以下为引用的内容:
package com.bjsxt.xml.training;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class XMLWriter {
public static void main(String[] args){
Element rootElt = new Element("selects"); //添加节点
Element selectElt = new Element("select");
Element idElt = new Element("id");
idElt.addContent("1"); //添加节点的值(可以用变量代替)
Element valueElt = new Element("name");
valueElt.addContent("福建省");
//明确节点之间的层次关系
selectElt.addContent(idElt);
selectElt.addContent(valueElt);
rootElt.addContent(selectElt);
//创建xml文件
Document doc = new Document(rootElt);
XMLOutputter out = new XMLOutputter();
String xmlStr = out.outputString(doc);
//System.out.println(xmlStr);
try {
out.output(doc, new FileOutputStream("c:/test.xml"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}