package com.yx.zzg.jdom;
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 WriteXML {
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);
Document doc = new Document(rootElt);
XMLOutputter out = new XMLOutputter();
// 默认UTF-8,这里可以设置
// out.setFormat(Format.getCompactFormat().setEncoding("gb2312"));
String xmlStr = out.outputString(doc);
System.out.println(xmlStr);
try {
out.output(doc, new FileOutputStream("c:/b/b.xml"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
利用JDom写XML
最新推荐文章于 2022-02-25 18:19:43 发布