浏览 2601 次
锁定老帖子 主题:dom4j 02
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (2)
|
|
---|---|
作者 | 正文 |
发表时间:2010-04-14
最后修改:2010-04-14
网上找了个不全的例子,经过自己测试已通过, 该例子主要实现的是对实体对象存储在xml文件中crud操作,但是没有可扩充性 目前只能存储单个对象,下一篇贴上可扩展的实现
1、实体对象
public class FriendsInfo { String name ; String gtalk; String address; String image; String QQ; String MSN; public FriendsInfo(String name, String address) { this.address =address; this.name = name; } public FriendsInfo() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGtalk() { return gtalk; } public void setGtalk(String gtalk) { this.gtalk = gtalk; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getQQ() { return QQ; } public void setQQ(String qQ) { QQ = qQ; } public String getMSN() { return MSN; } public void setMSN(String mSN) { MSN = mSN; } }
public class WriteFriendsInfo{ String filename=""; public void setFilename(String filename) { this.filename = filename; } public String getFilename() { return filename; } public WriteFriendsInfo(){ } public void createDocument(){ Document document = DocumentHelper.createDocument(); Element catalogElement = document.addElement("friends"); Element fmessage=catalogElement.addElement("message"); Element image=fmessage.addElement("image"); Element name=fmessage.addElement("name"); Element gtalk=fmessage.addElement("gtalk"); Element msn=fmessage.addElement("msn"); Element qq=fmessage.addElement("qq"); Element address=fmessage.addElement("address"); fmessage=catalogElement.addElement("message"); try{ XMLWriter output = new XMLWriter( new FileWriter(new File(filename))); output.write( document ); output.close(); } catch(IOException e){System.out.println(e.getMessage()); } saveXml(document); } public void delInfo(FriendsInfo fi){ Document document=null; SAXReader saxReader = new SAXReader(); try{ FileReader fr= new FileReader(new File(filename)); document = saxReader.read(fr); }catch(Exception e){ e.printStackTrace(); } List list = document.selectNodes("//friends/message/name"); Iterator iter = list.iterator(); while(iter.hasNext()){ System.out.print("###########\n"); if(iter.hasNext()){ Element ownerElement = (Element)iter.next(); if(ownerElement.getText().equals(fi.getName())) ownerElement.getParent().getParent().remove(ownerElement.getParent()); } } saveXml(document); } public void changeXml(FriendsInfo fi){ delInfo(fi); addXml(fi); } public void addXml(FriendsInfo fi){ delInfo(fi); org.dom4j.Document document=null; org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader(); try{ FileReader fr= new FileReader(new File(filename)); document = saxReader.read(fr); }catch(Exception e){ e.printStackTrace(); } Element catalogElement = document.getRootElement(); Element fmessage=catalogElement.addElement("message"); Element name=fmessage.addElement("name"); addEleText(name,fi.getName()); Element gtalk=fmessage.addElement("gtalk"); addEleText(gtalk,fi.getGtalk()); Element msn=fmessage.addElement("msn"); addEleText(msn,fi.getMSN()); Element qq=fmessage.addElement("qq"); addEleText(qq,fi.getQQ()); Element address=fmessage.addElement("address"); addEleText(address,fi.getAddress()); Element image=fmessage.addElement("image"); addEleText(image,fi.getImage()); saveXml(document); } public void addEleText(Element el,String text){ if(text!=null) el.addText(text); } public void saveXml(Document document ){ try{ XMLWriter output = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); output = new XMLWriter(new FileWriter(new File(filename)),format); output.write(document); output.close(); }catch(Exception ex){ ex.printStackTrace(); } } public void reBuildXml(){ createDocument(); addXml(createtmepinfo()); addXml(createtmepinfo1()); } public static void main(String[] argv){ String filename = "d:/test.xml"; WriteFriendsInfo dom4j = new WriteFriendsInfo(); dom4j.setFilename(filename); dom4j.createDocument(); dom4j.addXml(dom4j.createtmepinfo()); // dom4j.addXml(dom4j.createtmepinfo1()); // // dom4j.formatXMLFile(filename); // //dom4j.changeXml(dom4j.createtmepinfo1(),filename); // // dom4j.delInfo(dom4j.createtmepinfo1(), filename); // dom4j.reBuildXml(); // dom4j.printFile(filename); } public FriendsInfo createtmepinfo1(){ FriendsInfo friendsinfo=new FriendsInfo("gaoming","jiangsu"); friendsinfo.setImage("/images/text.png"); friendsinfo.setQQ("41895555"); friendsinfo.setMSN("ysen@gmail.com"); return friendsinfo; } public FriendsInfo createtmepinfo(){ FriendsInfo friendsinfo=new FriendsInfo("gao","lanyungang"); friendsinfo.setQQ("1235551"); friendsinfo.setImage("/images/text.png"); return friendsinfo; } public void printFile(String filename){ try { FileInputStream fis = new FileInputStream(filename); InputStreamReader dis = new InputStreamReader(fis); BufferedReader reader = new BufferedReader(dis); String s; while( (s = reader.readLine()) != null ) { System.out.println(s); } dis.close(); }catch(IOException e) { System.out.println(e); } } public int formatXMLFile(String filename){ int returnValue = 0; try{ SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File(filename)); XMLWriter output = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); output = new XMLWriter(new FileWriter( new File(getClass().getResource("/").toString()+"/kb2.xml") ),format); output.write(document); output.close(); returnValue = 1; }catch(Exception ex){ ex.printStackTrace(); } return returnValue; } }
public class ReadFriendsInfo { org.dom4j.Document document=null; public ReadFriendsInfo(String filename) { SAXReader saxReader = new SAXReader(); try{ FileReader fr= new FileReader(new File(filename)); document = saxReader.read(fr); }catch (DocumentException ex) { WriteFriendsInfo wi=new WriteFriendsInfo(); wi.reBuildXml(); new ReadFriendsInfo(filename); } catch(IOException e){ WriteFriendsInfo wi=new WriteFriendsInfo(); wi.reBuildXml(); new ReadFriendsInfo(filename); } } public Vector getInfo(){ Element root = document.getRootElement(); Vector v=new Vector(); for (Iterator i = root.elementIterator("message"); i.hasNext();){ Element foo = (Element) i.next(); FriendsInfo fi=new FriendsInfo(); fi.setName(foo.elementText("name")); if(foo.elementText("name")==null) continue; fi.setGtalk(foo.elementText("gtalk")); fi.setAddress(foo.elementText("address")); fi.setImage(foo.elementText("image")); fi.setQQ(foo.elementText("qq")); fi.setMSN(foo.elementText("msn")); fi.setImage(fi.getImage()); v.add(fi); } return v; } public static void main(String arge[]) { String filename = "d:/test.xml"; ReadFriendsInfo ri=new ReadFriendsInfo(filename); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |