?xmlversion=1.0encoding=GBK?书库书书名Java编程入门/书名作者张三/作者出版社电子出版社/出版社价格35.0/价格出版日期2002-10-07/出版日期/书书书名XML在Java中的应用/书名作者李四/作者出版社希望出版社/出版社价格92.0/价格出版日期2002-10-07/出版日期/书/书库下面是操作XML文件的Bean:packagexml;/***XML的读写操作Bean*/importjava.io.*;importjava.util.*;importorg.jdom.*;importorg.jdom.output.*;importorg.jdom.input.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassXmlBean{privateStringbookname,author,pub,price,pubdate;publicStringgetbookname(){returnbookname;}publicStringgetauthor(){returnauthor;}publicStringgetpub(){returnpub;}publicStringgetprice(){returnprice;}publicStringgetpubdate(){returnpubdate;}publicvoidsetbookname(Stringbookname){this.bookname=bookname;}publicvoidsetauthor(Stringauthor){this.author=author;}publicvoidsetpub(Stringpub){this.pub=pub;}publicvoidsetprice(Stringprice){this.price=price;}publicvoidsetpubdate(Stringpubdate){this.pubdate=pubdate;}publicXmlBean(){}/***读取XML文件所有信息*/publicVectorLoadXML(Stringpath)throwsException{VectorxmlVector=null;FileInputStreamfi=null;try{fi=newFileInputStream(path);xmlVector=newVector();SAXBuildersb=newSAXBuilder();Documentdoc=sb.build(fi);Elementroot=doc.getRootElement();//得到根元素Listbooks=root.getChildren();//得到根元素所有子元素的集合Elementbook=null;XmlBeanxml=null;for(inti=0;ibooks.size();i++){xml=newXmlBean();book=(Element)books.get(i);//得到第一本书元素xml.setbookname(book.getChild(书名).getText());xml.setauthor(book.getChild(作者).getText());xml.setpub(book.getChild(出版社).getText());xml.setprice(book.getChild(价格).getText());xml.setpubdate(book.getChild(出版日期).getText());xmlVector.add(xml);}}catch(Exceptione){System.err.println(e+error);}finally{try{fi.close();}catch(Exceptione){e.printStackTrace();}}returnxmlVector;}/***删除XML文件指定信息*/publicstaticvoidDelXML(HttpServletRequestrequest)throwsException{FileInputStreamfi=null;FileOutputStreamfo=null;try{Stringpath=request.getParameter(path);intxmlid=Integer.parseInt(request.getParameter(id));fi=newFileInputStream(path);SAXBuildersb=newSAXBuilder();Documentdoc=sb.build(fi);Elementroot=doc.getRootElement();//得到根元素Listbooks=root.getChildren();//得到根元素所有子元素的集合books.remove(xmlid);//删除指定位置的子元素Stringindent=;booleannewLines=true;XMLOutputteroutp=newXMLOutputter(indent,newLines,GBK);fo=newFileOutputStream(path);outp.output(doc,fo);}catch(Exceptione){System.err.println(e+error);}finally{try{fi.close();fo.close();}catch(Exceptione){e.printStackTrace();}}}/***添加XML文件指定信息*/publicstaticvoidAddXML(HttpServletRequestrequest)throwsException{FileInputStreamfi=null;FileOutputStreamfo=null;try{Stringpath=request.getParameter(path);fi=newFileInputStream(path);SAXBuildersb=newSAXBuilder();Documentdoc=sb.build(fi);Elementroot=doc.getRootElement();//得到根元素Listbooks=root.getChildren();//得到根元素所有子元素的集合Stringbookname=request.getParameter(bookname);Stringauthor=request.getParameter(author);Stringprice=request.getParameter(price);Stringpub=request.getParameter(pub);Stringpubdate=request.getParameter(pubdate);Textnewtext;Elementnewbook=newElement(书);Elementnewname=newElement(书名);newname.setText(bookname);newbook.addContent(newname);Elementnewauthor=newElement(作者);newauthor.setText(author);newbook.addContent(newauthor);Elementnewpub=newElement(出版社);newpub.setText(pub);newbook.addContent(newpub);Elementnewprice=newElement(价格);newprice.setText(price);newbook.addContent(newprice);Elementnewdate=newElement(出版日期);newdate.setText(pubdate);newbook.addContent(newdate);books.add(newbook);//增加子元素Stringindent=;booleannewLines=true;XMLOutputteroutp=newXMLOutputter(indent,newLines,GBK);fo=newFileOutputStream(path);outp.output(doc,fo);}catch(Exceptione){System.err.println(e+error);}finally{try{fi.close();fo.close();}catch(Exceptione){e.printStackTrace();}}}/***修改XML文件指定信息*/publicstaticvoidEditXML(HttpServletRequestrequest)throwsException{FileInputStreamfi=null;FileOutputStreamfo=null;try{Stringpath=request.getParameter(path);intxmlid=Integer.parseInt(request.getParameter(id));fi=newFileInputStream(path);SAXBuildersb=newSAXBuilder();Documentdoc=sb.build(fi);Elementroot=doc.getRootElement();//得到根元素Listbooks=root.getChildren();//得到根元素所有子元素的集合Elementbook=(Element)books.get(xmlid);Stringbookname=request.getParameter(bookname);Stringauthor=request.getParameter(author);Stringprice=request.getParameter(price);Stringpub=request.getParameter(pub);Stringpubdate=request.getParameter(pubdate);Textnewtext;Elementnewname=book.getChild(书名);newname.setText(bookname);//修改书名为新的书名Elementnewauthor=book.getChild(作者);newauthor.setText(author);Elementnewpub=book.getChild(出版社);newpub.setText(pub);Elementnewprice=book.getChild(价格);newprice.setText(price);Elementnewdate=book.getChild(出版日期);newdate.setText(pubdate);//books.set(xmlid,book);//修改子元素Stringindent=;booleannewLines=true;XMLOutputteroutp=newXMLOutputter(indent,newLines,GBK);fo=newFileOutputStream(path);outp.output(doc,fo);}catch(Exceptione){System.err.println(e+error);}finally{try{fi.close();fo.close();}catch(Exceptione){e.printStackTrace();}}}}下面是操作的jsp文件:%@pagecontentType=text/html;charset=GB