码迷,mamicode.com
首页 > 其他好文 > 详细

通过 JDOM 方式生成 XML 文档

时间:2015-01-15 15:48:33      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1.引入jdom jar包

2.代码

     Element ele = new Element("tree");
        ele.setAttribute("id","0");
        
        Document document = new Document(ele);
        
        Element e1 = new Element("name");
        e1.setText("名称");
        ele.addContent(e1);
    
//        Format format = Format.getCompactFormat();//xml格式
        Format format = Format.getPrettyFormat();//xml格式
//        format.setIndent("");//设置换行,getPrettyFormat不用设
        format.setEncoding("GBK");
        
        XMLOutputter outputter = new XMLOutputter(format);
        try {
            outputter.output(document, new FileOutputStream(new File("jdomTest.xml")));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

 

3.结果

<?xml version="1.0" encoding="GBK"?>
<tree id="0">
  <name>名称</name>
</tree>

 

通过 JDOM 方式生成 XML 文档

标签:

原文地址:http://www.cnblogs.com/hebin-javaemperor/p/4226261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!