先下载jdom包。加入环境变量:
然后看程序:
package jdomstury;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: xxxxxxxxxxxxxxxxxxx</p>
 * @author csk
 * @version 1.0
 */import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.util.*;
import java.math.*;public class test {
  public void addNewBook(Element root,String name,String author,String date,String price)
  {
    try {
      Element  newbook=new Element("book");
      newbook.addContent(new Element("name"));
      newbook.addContent(new Element("author"));
      newbook.addContent(new Element("publishDate"));
      newbook.addContent(new Element("price"));
    //  List ls=newbook.getChildren();
      Element name1=(Element)newbook.getChild("name");
    //  Element name1=e.getChild("name");
      name1.setText(name);
      Element author1=newbook.getChild("author");
      author1.setText(author);
      Element publishDate1=newbook.getChild("publishDate");
      publishDate1.setText(date);
      Element price1=newbook.getChild("price");
      price1.setText(price);
      root.addContent(newbook);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }  }
  public void delBook(Element root,String name)
  {    List booklist=root.getChildren();
    System.out.println(booklist.size());
    for(int i=0;i<booklist.size();i++)
    {
      System.out.println("boolist["+i+"]");
      Element delbook=(Element)booklist.get(i);
      String delbookname=null;
      delbookname=delbook.getChild("name").getText().trim();
      System.out.println("bookname="+delbookname);
      if(delbookname.equals(name))
        System.out.println(root.removeContent(delbook));
    }
  }
  public ArrayList searchBook(Element root,String bookname)
  {
    List  list=root.getChildren();
    ArrayList booklist=new ArrayList();
    Iterator it= list.iterator();
    Element book=null;
    while(it.hasNext())
    {
     book=(Element)it.next();
      if(book.getChildText("name").equals(bookname))
       booklist.add(book);
    }
     return booklist;
  }
  public test() {
  }
  public static void main(String[] args) {
    test test1 = new test();
    try {
      SAXBuilder sb = new SAXBuilder();      //从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
      Document doc = sb.build(new FileInputStream("F:\\exampleA.xml"));      //加入一条处理指令
      ProcessingInstruction pi = new ProcessingInstruction
      ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
      doc.addContent(pi);
      Element root=doc.getRootElement();
      test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
      test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
    // test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
      // test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
      test1.delBook(root,"java jdom");
     ArrayList booklist=test1.searchBook(root,"Java编程入门");
     for(int i=0;i<booklist.size();i++)
     {
       Element searchbook=(Element)booklist.get(i);
      if(searchbook!=null)
      {
        System.out.println("name="+searchbook.getChildText("name"));
        System.out.println("author="+searchbook.getChildText("author"));
        System.out.println("publishDate="+searchbook.getChildText("publishDate"));
        System.out.println("price="+searchbook.getChildText("price"));      }
     }
      String indent = "    ";
      boolean newLines = false;
      XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
      outp.output(doc, new FileOutputStream("F:\\exampleB.xml"));
    }
    catch (JDOMException ex) {
    }catch (IOException ex) {
    }
  }
}再把那个例子的exampleA.xml给你:
<?xml version="1.0" encoding="GBK"?> 
<bookList> 
   <book> 
       <name>Java编程入门</name> 
       <author>张三</author> 
       <publishDate>2002-6-6</publishDate> 
       <price>35.0</price> 
   </book> 
   <book> 
       <name>XML在Java中的应用</name> 
       <author>李四</author> 
       <publishDate>2002-9-16</publishDate> 
       <price>92.0</price> 
   </book> 
<book> 
       <name>Java编程入门</name> 
       <author>张三</author> 
       <publishDate>2002-6-6</publishDate> 
       <price>35.0</price>    </book> </bookList> 好了,运行你的程序吧。注意:1.包的位置,2.你的目标文件xampleA.xml的路径。

解决方案 »

  1.   

    我调试过了 ! 你的应用程序可以运行的!但是 我想知道怎么用jsp 直接对xml 进行修改! 而且你的方法没有修改数据的功能! 除非先 del 然后再 add 那样做可太费事了!!另外 应用程序不同于web端程序 , 我们对安全级别要求的比较高!! 你在客户端 可以随便操作xml 的配置文件么????希望你帮我看看.......!!
    为了表示感谢 上面的分都给你吧! ^_^
      

  2.   

    正好是我需要的,谢谢 shaokun305(混口饭吃!) 
    不过,Jdom 写出来的XML文件,怎么没有换行呢?