jdom操作xml文件基础

      首先是个xml文件

xml 代码
  1. xml version='1.0' encoding='utf-8'?>  
  2. <书库>  
  3.     <>  
  4.         <书名>Java编程入门书名>  
  5.         <作者>张三作者>  
  6.         <出版社>电子出版社出版社>  
  7.         <价格>35.0价格>  
  8.         <出版日期>2002-10-07出版日期>  
  9.     >  
  10.     <>  
  11.         <书名>XML在Java中的应用书名>  
  12.         <作者>李四作者>  
  13.         <出版社>希望出版社出版社>  
  14.         <价格>92.0价格>  
  15.         <出版日期>2002-10-07出版日期>  
  16.     >  
  17. 书库>  
 
接下来是类
java 代码
  1. package jdom.test;   
  2.   
  3. public class Book   
  4. {   
  5.     private String bookname, author, pub, price, pubdate;   
  6.   
  7.     public Book(){}   
  8.        
  9.     public Book(String bookname, String author, String pub, String price, String pubdate)   
  10.     {   
  11.         this.bookname = bookname;   
  12.         this.author = author;   
  13.         this.pub = pub;   
  14.         this.price = price;   
  15.         this.pubdate = pubdate;   
  16.     }   
  17.        
  18.     public String getAuthor()   
  19.     {   
  20.         return author;   
  21.     }   
  22.   
  23.     public void setAuthor(String author)   
  24.     {   
  25.         this.author = author;   
  26.     }   
  27.   
  28.     public String getBookname()   
  29.     {   
  30.         return bookname;   
  31.     }   
  32.   
  33.     public void setBookname(String bookname)   
  34.     {   
  35.         this.bookname = bookname;   
  36.     }   
  37.   
  38.     public String getPrice()   
  39.     {   
  40.         return price;   
  41.     }   
  42.   
  43.     public void setPrice(String price)   
  44.     {   
  45.         this.price = price;   
  46.     }   
  47.   
  48.     public String getPub()   
  49.     {   
  50.         return pub;   
  51.     }   
  52.   
  53.     public void setPub(String pub)   
  54.     {   
  55.         this.pub = pub;   
  56.     }   
  57.   
  58.     public String getPubdate()   
  59.     {   
  60.         return pubdate;   
  61.     }   
  62.   
  63.     public void setPubdate(String pubdate)   
  64.     {   
  65.         this.pubdate = pubdate;   
  66.     }   
  67. }   

       然后是对这个类访问
       java 代码

  1. package jdom.test;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7. import java.util.List;   
  8. import java.util.Vector;   
  9.   
  10. import org.jdom.Document;   
  11. import org.jdom.Element;   
  12. import org.jdom.JDOMException;   
  13. import org.jdom.input.SAXBuilder;   
  14. import org.jdom.output.Format;   
  15. import org.jdom.output.XMLOutputter;   
  16.   
  17. public class XmlBean   
  18. {   
  19.     Document doc;   
  20.     Book book;   
  21.        
  22.     public XmlBean(String path, Book book)   
  23.     {   
  24.         getDocument(path);   
  25.         this.book = book;   
  26.     }   
  27.        
  28.     public XmlBean(String path)   
  29.     {   
  30.         getDocument(path);   
  31.         book = new Book();   
  32.     }   
  33.        
  34.     /**  
  35.      * 读取XML文件所有信息  
  36.      * @return Vector<book></book>  
  37.      */  
  38.     public Vector<book></book> LoadXML()   
  39.     {   
  40.         List books = getList();   
  41.         Vector<book></book> vector = new Vector<book></book>();   
  42.         for (int i = 0; i < books.size(); i++)   
  43.         {   
  44.             Book book = new Book();   
  45.             Element element = (Element) books.get(i);   
  46.             book.setBookname(element.getChild("书名").getText());   
  47.             book.setAuthor(element.getChild("作者").getText());   
  48.             book.setPub(element.getChild("出版社").getText());   
  49.             book.setPrice(element.getChild("价格").getText());   
  50.             book.setPubdate(element.getChild("出版日期").getText());   
  51.             vector.add(book);   
  52.         }   
  53.         return vector;   
  54.     }   
  55.   
  56.     /**  
  57.      * 删除XML文件指定信息  
  58.      * @param request  
  59.      */  
  60.     public void delXML(String path, int index)   
  61.     {   
  62.         List books = getList();   
  63.         books.remove(index);   
  64.         docToFile(path);   
  65.     }   
  66.   
  67.     /**  
  68.      * 向XML文件添加信息  
  69.      * @param path  
  70.      */  
  71.     public void addXML(String path)   
  72.     {   
  73.         List<element></element> books = getList();   
  74.         Element newbook = new Element("书");   
  75.         Element newname = new Element("书名");   
  76.         newname.setText(book.getBookname());   
  77.         newbook.addContent(newname);   
  78.         Element newauthor = new Element("作者");   
  79.         newauthor.setText(book.getAuthor());   
  80.         newbook.addContent(newauthor);   
  81.         Element newpub = new Element("出版社");   
  82.         newpub.setText(book.getPub());   
  83.         newbook.addContent(newpub);   
  84.         Element newprice = new Element("价格");   
  85.         newprice.setText(book.getPrice());   
  86.         newbook.addContent(newprice);   
  87.         Element newdate = new Element("出版日期");   
  88.         newdate.setText(book.getPubdate());   
  89.         newbook.addContent(newdate);   
  90.         books.add(newbook);   
  91.         docToFile(path);   
  92.     }   
  93.        
  94.     /**  
  95.      * 修改XML文件指定信息  
  96.      * @param request  
  97.      */  
  98.     public void editXML(String path, int index)   
  99.     {   
  100.         List books = getList();   
  101.         Element element = (Element) books.get(index);   
  102.         Element newname = element.getChild("书名");   
  103.         newname.setText(book.getBookname());   
  104.         Element newauthor = element.getChild("作者");   
  105.         newauthor.setText(book.getAuthor());   
  106.         Element newpub = element.getChild("出版社");   
  107.         newpub.setText(book.getPub());   
  108.         Element newprice = element.getChild("价格");   
  109.         newprice.setText(book.getPrice());   
  110.         Element newdate = element.getChild("出版日期");   
  111.         newdate.setText(book.getPubdate());   
  112.         docToFile(path);   
  113.     }   
  114.            
  115.     /**  
  116.      * 写入文件  
  117.      * @param path  
  118.      */  
  119.     void docToFile(String path)   
  120.     {   
  121.         Format format = Format.getPrettyFormat();   
  122.         format.setIndent("");   
  123.         format.setEncoding("utf-8");   
  124.         XMLOutputter outp = new XMLOutputter(format);   
  125.         FileOutputStream fo = null;   
  126.         try  
  127.         {   
  128.             fo = new FileOutputStream(path);   
  129.             outp.output(doc, fo);   
  130.         }   
  131.         catch (FileNotFoundException e)   
  132.         {   
  133.             e.printStackTrace();   
  134.         }   
  135.         catch (IOException e)   
  136.         {   
  137.             e.printStackTrace();   
  138.         }   
  139.         finally  
  140.         {   
  141.             close(fo);   
  142.         }   
  143.     }   
  144.        
  145.     /**  
  146.      *   
  147.      * @param path  
  148.      */  
  149.     void getDocument(String path)   
  150.     {   
  151.         FileInputStream fi = null;   
  152.         try  
  153.         {   
  154.             fi = new FileInputStream(path);   
  155.             SAXBuilder sb = new SAXBuilder();   
  156.             doc = sb.build(fi);   
  157.         }   
  158.         catch (FileNotFoundException e)   
  159.         {   
  160.             e.printStackTrace();   
  161.         }   
  162.         catch (JDOMException e)   
  163.         {   
  164.             e.printStackTrace();   
  165.         }   
  166.         catch (IOException e)   
  167.         {   
  168.             e.printStackTrace();   
  169.         }   
  170.         finally  
  171.         {   
  172.             close(fi);   
  173.         }   
  174.     }   
  175.        
  176.     /**  
  177.      * 得到根元素所有子元素的集合  
  178.      * @param path  
  179.      * @return  
  180.      */  
  181.     List<element></element> getList()   
  182.     {   
  183.         Element root = doc.getRootElement();   
  184.         List<element></element> list = root.getChildren();   
  185.         return list;   
  186.     }   
  187.        
  188.     void close(FileInputStream fi)   
  189.     {   
  190.         if (fi != null)   
  191.         {   
  192.             try  
  193.             {   
  194.             fi.close();   
  195.             }   
  196.             catch (IOException e)   
  197.             {   
  198.             e.printStackTrace();   
  199.             }   
  200.             fi = null;   
  201.         }   
  202.     }   
  203.        
  204.     void close(FileOutputStream fo)   
  205.     {   
  206.         if (fo != null)   
  207.         {   
  208.             try  
  209.             {   
  210.             fo.close();   
  211.             }   
  212.             catch (IOException e)   
  213.             {   
  214.             e.printStackTrace();   
  215.             }   
  216.             fo = null;   
  217.         }   
  218.     }   
  219. }  

再来分别是servlet和filter

java 代码
  1. package servlet;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.PrintWriter;   
  5.   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.http.HttpServlet;   
  8. import javax.servlet.http.HttpServletRequest;   
  9. import javax.servlet.http.HttpServletResponse;   
  10.   
  11. import jdom.test.Book;   
  12. import jdom.test.XmlBean;   
  13.   
  14. public class XmlServlet extends HttpServlet   
  15. {   
  16.     private static final long serialVersionUID = 1L;   
  17.   
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
  19.     {   
  20.         doPost(request, response);   
  21.     }   
  22.        
  23.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
  24.     {   
  25.         PrintWriter out = response.getWriter();   
  26.         String result = "";   
  27.         String act = request.getParameter("act");   
  28.         String path = request.getParameter("path");   
  29.         if ("add".equals(act))   
  30.         {   
  31.             String bookname = request.getParameter("bookname");   
  32.             String author = request.getParameter("author");   
  33.             String pub = request.getParameter("pub");   
  34.             String price = request.getParameter("price");   
  35.             String pubdate = request.getParameter("pubdate");   
  36.             Book book = new Book(bookname, author, pub, price, pubdate);   
  37.             XmlBean xb = new XmlBean(path, book);   
  38.             xb.addXML(path);   
  39.             result = "添加成功";   
  40.         }   
  41.         else if ("del".equals(act))   
  42.         {   
  43.             int id = Integer.parseInt(request.getParameter("id"));   
  44.             XmlBean xb = new XmlBean(path);   
  45.             xb.delXML(path, id);   
  46.             result = "删除成功";   
  47.         }   
  48.         else if ("edit".equals(act))   
  49.         {   
  50.             int id = Integer.parseInt(request.getParameter("id"));   
  51.             String bookname = request.getParameter("bookname");   
  52.             String author = request.getParameter("author");   
  53.             String pub = request.getParameter("pub");   
  54.             String price = request.getParameter("price");   
  55.             String pubdate = request.getParameter("pubdate");   
  56.             Book book = new Book(bookname, author, pub, price, pubdate);   
  57.             XmlBean xb = new XmlBean(path, book);   
  58.             xb.editXML(path, id);   
  59.             result = "修改成功";   
  60.         }   
  61.         else  
  62.         {   
  63.             result = "非法操作";   
  64.         }   
  65.         out.print("



    " + result + "

    返回");

  66.     }   
  67. }   
java 代码
  1. package filter;   
  2.   
  3. import java.io.IOException;   
  4.   
  5. import javax.servlet.Filter;   
  6. import javax.servlet.FilterChain;   
  7. import javax.servlet.FilterConfig;   
  8. import javax.servlet.ServletException;   
  9. import javax.servlet.ServletRequest;   
  10. import javax.servlet.ServletResponse;   
  11.   
  12. public class CharsetFilter implements Filter   
  13. {   
  14.     public void destroy()   
  15.     {}   
  16.   
  17.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException   
  18.     {   
  19.         request.setCharacterEncoding("GBK");   
  20.         response.setCharacterEncoding("GBK");   
  21.         chain.doFilter(request, response);   
  22.     }   
  23.   
  24.     public void init(FilterConfig arg0) throws ServletException   
  25.     {}   
  26. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值