newdate.setText(pubdate); newbook.addContent(newdate); books.add(newbook);//增加子元素 String indent = \boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,\fo=new FileOutputStream(path); outp.output(doc,fo); }
catch(Exception e){
System.err.println(e+\}
finally{ try{
fi.close(); fo.close(); }
catch(Exception e){ e.printStackTrace(); } } } /**
* 修改XML文件指定信息 */
public static void EditXML(HttpServletRequest request)throws Exception{ FileInputStream fi = null; FileOutputStream fo = null; try{
String path=request.getParameter(\
int xmlid=Integer.parseInt(request.getParameter(\fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi);
Element root = doc.getRootElement(); //得到根元素
List books = root.getChildren(); //得到根元素所有子元素的集合 Element book=(Element)books.get(xmlid);
String bookname=request.getParameter(\String author=request.getParameter(\String price=request.getParameter(\String pub=request.getParameter(\
String pubdate=request.getParameter(\Text newtext;
Element newname= book.getChild(\书名\
newname.setText(bookname);//修改书名为新的书名 Element newauthor= book.getChild(\作者\newauthor.setText(author);
Element newpub= book.getChild(\出版社\newpub.setText(pub);
Element newprice= book.getChild(\价格\newprice.setText(price);
Element newdate= book.getChild(\出版日期\newdate.setText(pubdate);
//books.set(xmlid,book);//修改子元素 String indent = \boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,\fo=new FileOutputStream(path); outp.output(doc,fo); }
catch(Exception e){
System.err.println(e+\}
finally{ try{
fi.close(); fo.close(); }
catch(Exception e){ e.printStackTrace(); } } } }
下面是操作的jsp文件:
<%@ page contentType=\
<%@ page language=\
String path = application.getRealPath(\XmlBean xml=new XmlBean();
Vector xmlall=xml.LoadXML(path); for(int i=0;i /**out.println(\书名:\out.println(\作者:\out.println(\出版社:\out.println(\价格:\ out.println(\出版日期:\*/ %> style=\ href=\删除
<%}%>
相关推荐: