首页 > 其他 > 详细

XML文件生成——借助JDOM

时间:2014-12-08 19:29:28      阅读:261      评论:0      收藏:0      [点我收藏+]
 1 import java.io.* ;
 2 import org.jdom.* ;
 3 import org.jdom.output.* ;
 4 public class DOMDemo {
 5     public static void main(String args[]) throws Exception {
 6         Element addresslist = new Element("addresslist") ;
 7         Element linkman = new Element("linkman") ;
 8         Element name = new Element("name") ;
 9         Element email = new Element("email") ;
10         Attribute id = new Attribute("id","lxh") ;
11         Document doc = new Document(addresslist) ;    // 定义Document对象
12         name.setText("李兴华") ;
13         name.setAttribute(id) ;    // 将属性设置到元素之中
14         email.setText("mldnqa@163.com") ;
15         linkman.addContent(name) ;    // 设置关系
16         linkman.addContent(email) ;
17         addresslist.addContent(linkman) ;
18         XMLOutputter out = new XMLOutputter() ;
19         out.setFormat(out.getFormat().setEncoding("GBK")) ;    // 表示的是设置编码
20         out.output(doc,new FileOutputStream(new File("D:" + File.separator + "address.xml"))) ;
21     }
22 }

要求在工程中引入jdom.jar

XML文件生成——借助JDOM

原文:http://www.cnblogs.com/hixin/p/4151635.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!