首页 > 代码库 > XML文件生成——借助JDOM

XML文件生成——借助JDOM

 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