使用jdom组装xml
时间:2011-02-20 来源:魔战
public class XMLWriter {
public static void main(String args[])
{
Element root=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);
root.addContent(selectElt);
Document doc=new Document(rootElt);
XMLOutputter out=new XMLOutputter();
//设置输出的编码
out.setFormat(Format.getCompactFormat().setEncoding("gb2312"));
//输出字符串
String xmlStr=out.outputString(doc);
System.out.println(xmlStr);
//保存在文件中
out.output(doc,new FileOutputStream("c:/test.xml"));
}
}