//exampleA.xml
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book>
<name>Java编程</name>
<author>张三</author>
<publishDate>2005-10-28</publishDate>
<price>55.0</price>
</book>
<book>
<name>J2EE开发应用</name>
<author>李四</author>
<publishDate>2005-9-16</publishDate>
<price>100.0</price>
</book>
</bookList>
------------------------------------------------------------------------
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.ProcessingInstruction;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
* @param gongtao
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
// 创建文档
Document doc=sb.build(new FileInputStream("c:\\exampleA.xml"));
// 加入一条处理指令
org.jdom.ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
// 把这条处理指令,加入文档中
doc.addContent(pi);
// 获得这个文档得跟元素
Element el=doc.getRootElement();
// 获得这个跟元素,的所有子元素
java.util.List ls=el.getChildren();
// 得到第一个子元素
Element book=(Element)ls.get(0);
// 给这个字元素添加一条属性
Attribute attr=new Attribute("hot","true");
book.setAttribute(attr);
Element el2=book.getChild("author");
// 输出这个元素的值
System.out.println(el2.getName());
// 给这个元素的值改个名字
el2.setText("cute");
Element el3=book.getChild("price");
// 给这个值换个值
el3.setText(Float.toString(50.0f));
Format format = Format.getPrettyFormat();
format.setEncoding("GB2312");
XMLOutputter xml=new XMLOutputter();
xml.setFormat(format);
xml.output(doc,new FileOutputStream("c:\\cute.xml"));
catch(Exception e){
System.out.println(e.getMessage());
}