|
Java + XML = JDOM ! 这就是JDOM设计者的目标。如果你曾经使用过烦人的SAX或是DOM来处理XML,你就会知道为什么要有JDOM或者是JAXB。在今年(2002)的JavaOne会议上JDOM的主要创始人Jason Hunter有一篇精彩的演讲介绍了JDOM技术,题目就是JDOM Makes XML Easy。 获得并安装JDOM 在http://jdom.org可以下载JDOM的最新版本。以JDOM beta8的2进制版本为例。下载后解压缩,JDOM的jar文件就是build目录下的文件jdom.jar,将之加入类路径。另外JDOM还需要lib目录下那些jar文件如xerces.jar,jaxp.jar的支持。如果在使用中出现以下错误: java.lang.NoSuchMethodError 或 java.lang.NoClassDefFoundError: org/xml/sax/SAXNotRecognizedException 你需要保证xerces.jar文件在CLASSPATH中位于其他XML类,如JAXP或Crimson之前,这些类文件,包括以前老版本的xerces,可能不支持SAX2.0或DOM Level 2。于是导致了上面的错误。
一个简单的例子 JDOM的处理方式有些类似于DOM,但它主要是用SAX实现的,你不必担心处理速度和内存的问题。另外,JDOM中几乎没有接口,的类全部是实实在在的类,没有类工厂类的。
下面是实例用的XML文件:
<?xml version="1.0" encoding="GBK"?> <书库> <书> <书名>Java编程入门</书名> <作者>张三</作者> <出版社>电子出版社</出版社> <价格>35.0</价格> <出版日期>2002-10-07</出版日期> </书> <书> <书名>XML在Java中的应用</书名> <作者>李四</作者> <出版社>希望出版社</出版社> <价格>92.0</价格> <出版日期>2002-10-07</出版日期> </书> </书库>
下面是操作XML文件的Bean: package xml; /** * XML的读写操作Bean */ import java.io.*; import java.util.*; import org.jdom.*; import org.jdom.output.*; import org.jdom.input.*; import javax.servlet.*; import javax.servlet.http.*; public class XmlBean{ private String bookname,author,pub,price,pubdate; public String getbookname() { return bookname;} public String getauthor() { return author;} public String getpub() { return pub;} public String getprice() { return price;} public String getpubdate() { return pubdate;} public void setbookname(String bookname) { this.bookname =bookname ; } public void setauthor(String author) { this.author =author; } public void setpub(String pub) { this.pub =pub ; } public void setprice(String price) { this.price =price ; } public void setpubdate(String pubdate) { this.pubdate =pubdate ; } public XmlBean(){} /** * 读取XML文件所有信息 */ public Vector LoadXML(String path)throws Exception{ Vector xmlVector = null; FileInputStream fi = null; try{ fi = new FileInputStream(path); xmlVector = new Vector(); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); //得到根元素 List books = root.getChildren(); //得到根元素所有子元素的集合 Element book =null; XmlBean xml =null; for(int i=0;i<books.size();i++){ xml = new XmlBean(); 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(Exception e){ System.err.println(e+"error"); } finally{ try{ fi.close(); } catch(Exception e){ e.printStackTrace(); } } return xmlVector; } /** * 删除XML文件指定信息 */ public static void DelXML(HttpServletRequest request)throws Exception{ FileInputStream fi = null; FileOutputStream fo = null; try{ String path=request.getParameter("path"); int xmlid=Integer.parseInt(request.getParameter("id")); fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); //得到根元素 List books = root.getChildren(); //得到根元素所有子元素的集合 books.remove(xmlid);//删除指定位置的子元素 String indent = " "; boolean newLines = true; XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK"); fo=new FileOutputStream(path); outp.output(doc,fo); } catch(Exception e){ System.err.println(e+"error"); } finally{ try{ fi.close(); fo.close(); } catch(Exception e){ e.printStackTrace(); } } } /** * 添加XML文件指定信息 */ public static void AddXML(HttpServletRequest request)throws Exception{ FileInputStream fi = null; FileOutputStream fo = null; try{ String path=request.getParameter("path"); fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); //得到根元素 List books = root.getChildren(); //得到根元素所有子元素的集合 String bookname=request.getParameter("bookname"); String author=request.getParameter("author"); String price=request.getParameter("price"); String pub=request.getParameter("pub"); String pubdate=request.getParameter("pubdate"); Text newtext; Element newbook= new Element("书"); Element newname= new Element("书名"); newname.setText(bookname); newbook.addContent(newname); Element newauthor= new Element("作者"); newauthor.setText(author); newbook.addContent(newauthor); Element newpub= new Element("出版社"); newpub.setText(pub); newbook.addContent(newpub); Element newprice= new Element("价格"); newprice.setText(price); newbook.addContent(newprice); Element newdate= new Element("出版日期"); newdate.setText(pubdate); newbook.addContent(newdate); books.add(newbook);//增加子元素 String indent = " "; boolean newLines = true; XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK"); fo=new FileOutputStream(path); outp.output(doc,fo); } catch(Exception e){ System.err.println(e+"error"); } 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("path"); int xmlid=Integer.parseInt(request.getParameter("id")); 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("bookname"); String author=request.getParameter("author"); String price=request.getParameter("price"); String pub=request.getParameter("pub"); String pubdate=request.getParameter("pubdate"); 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,"GBK"); fo=new FileOutputStream(path); outp.output(doc,fo); } catch(Exception e){ System.err.println(e+"error"); } finally{ try{ fi.close(); fo.close(); } catch(Exception e){ e.printStackTrace(); } } } }
|
一共有 1 条评论