public class TestXml {
public void bulidXml() throws FileNotFoundException, IOException{
//创建根节点
Element root = new Element("game");
//添加根节点到文档中
Document doc = new Document(root);
root.addContent(new Element("gameDesc").setText("aaaa"));
root.addContent(new Element("gwAddres").setText("19"));
root.addContent(new Element("ltAddress").setText("女"));
root.addContent(new Element("czAddress").setText("女"));
root.addContent(new Element("vipDescAddress").setText("女"));
root.addContent(new Element("scTitle").setText("女"));
root.addContent(new Element("scAddress").setText("女"));
root.addContent(new Element("fcmAddress").setText("女"));
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8")); //设置字符集
xmlOut.output(doc, new FileOutputStream("user.xml"));
System.out.println(xmlOut.outputString(doc));
File file = new File("d:/test");
if(!file.exists()){ // 如果不存在此文件创建文件夹
file.mkdir();
}
File f = new File("d:/test/user.xml"); //创建文件夹中的文件
if(!f.exists()){
f.createNewFile();
}
//BufferedWriter bw = new BufferedWriter(fw,"");
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream("d:/test/user.xml"),"UTF-8");
xmlOut.output(doc, ow);
ow.close();
}
public static void main(String[] args) throws FileNotFoundException, IOException {
TestXml test = new TestXml();
System.out.println("生成 xml 文件。。。。。");
test.bulidXml();
}
利用jdom生成XML文件
原创hdycfm ©著作权
文章标签 利用jdom生成xml文件 文章分类 Java 后端开发
©著作权归作者所有:来自51CTO博客作者hdycfm的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
JDom,jdom解析xml文件
1.要解析的文件模板如下:
xml 文档 对象 类 jar