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("[email protected]") ; 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
时间: 2024-11-05 21:11:34