
JavaDOC的网址:http://www.jdom.org/docs/apidocs/index.html

import java.io.FileOutputStream;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.Attributes;
public class WriteXML {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//建立各个操作节点
Element addresslist = new Element("addresslist");
Element linkman = new Element("linkman");
Element name = new Element("name");
Element email = new Element("email");
//定义属性
Attribute id = new Attribute("id","zs");
//声明一个Document对象
Document doc = new Document(addresslist);
//设置元素的内容
name.setText("张三");
name.setAttribute(id); //设置name的属性
email.setText("www.baidu.com");
//设置linkman的子节点
linkman.addContent(name);
linkman.addContent(email);
//将linkman加入根节点中
addresslist.addContent(linkman);
//用来输出XML文件
XMLOutputter out = new XMLOutputter();
//设置输出的编码
out.setFormat(out.getFormat().setEncoding("UTF-8"));
//输出XML文件
try{
out.output(doc, new FileOutputStream("/home/common/software/coding/HelloWord/JavaWeb/bin/address.xml"));
}catch(Exception e){
e.printStackTrace();
}
}
}


原文:http://www.cnblogs.com/tonglin0325/p/5344399.html