- 浏览: 122220 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
roychenyi:
<br>1<br>2<br> ...
pager-taglib 使用说明 -
roychenyi:
<br>换行<br>换行<br& ...
pager-taglib 使用说明 -
wangwenfei1985:
[flash=200,200][url][img][list] ...
pager-taglib 使用说明 -
时光后19:
,看到你这样真好,
FileNet调用webService配置 -
ysen:
大sql导入mysql
如果是SQL格式的。有的可能上100 ...
mysqldump备份详解
通过将上一篇的更改,将Java对象映射到xml文件中扩展性增强!
1、想看两个简单的实体类
public class FriendsInfo extends BasePojo{ 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 BookInfo extends BasePojo{ String name = ""; String title = ""; String context =""; String price = ""; String show = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getShow() { return show; } public void setShow(String show) { this.show = show; }
2、两个实体类的父类
public class BasePojo { }
3、处理所需要的介面
public interface DOMExecute { public void addInfo(BasePojo basepojo); public void delInfo(BasePojo basepojo); public void changeInfo(BasePojo basepojo); public List getinfo(); public void createDocument( String dir); }
4、用到的适配器
public class EnumerationAdapter implements Iterator { private Enumeration enumeration; EnumerationAdapter(Enumeration enumeration) { this.enumeration = enumeration; } // 转接介面 public boolean hasNext() { return enumeration.hasMoreElements(); } public Object next() { return enumeration.nextElement(); } public void remove() { } }
5、处理类的父类
public abstract class baseDomHander implements DOMExecute{ ModeDocumentFactory modeDocumentFactory; private String dir = modeDocumentFactory.getDIR(); public baseDomHander(ModeDocumentFactory modeFactory){ modeDocumentFactory = modeFactory; } public ModeDocumentFactory getModeDocumentFactory() { return modeDocumentFactory; } public void setModeDocumentFactory(ModeDocumentFactory modeDocumentFactory) { this.modeDocumentFactory = modeDocumentFactory; } public String getDir() { return dir; } public void setDir(String dir) { this.dir = dir; } public void saveXml(Document document,String dir){ try{ XMLWriter output = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); output = new XMLWriter(new FileWriter(new File(dir)),format); output.write(document); output.close(); }catch(Exception ex){ ex.printStackTrace(); } } public void addEleText(Element el, String text) { if(text!=null) el.addText(text); }
6、Bookinfo的处理类
public class bookDomHander extends baseDomHander{ private String dir ; public bookDomHander(ModeDocumentFactory modeFactory) { super(modeFactory); } @Override public void delInfo(BasePojo basepojo) { BookInfo bi = (BookInfo)basepojo; Document document=null; SAXReader saxReader = new SAXReader(); try{ FileReader fr= new FileReader(new File(getDir())); document = saxReader.read(fr); }catch(Exception e){ e.printStackTrace(); } List list = document.selectNodes("//books/book/name"); Iterator iter = list.iterator(); while(iter.hasNext()){ System.out.print("###########\n"); if(iter.hasNext()){ Element ownerElement = (Element)iter.next(); if(ownerElement.getText().equals(bi.getName())) ownerElement.getParent().getParent().remove(ownerElement.getParent()); } } saveXml(document, getDir()); } @Override public List getinfo() { return null; } @Override public void createDocument( String dir) { setDir(dir); Document document = DocumentHelper.createDocument(); Element books = document.addElement("books"); Element book=books.addElement("book"); Element title=book.addElement("title"); title.addAttribute("show", "yes"); Element context=book.addElement("context"); Element qq=book.addElement("qq"); Element address=book.addElement("price"); try{ XMLWriter output = new XMLWriter( new FileWriter(new File(dir))); output.write( document ); output.close(); } catch(IOException e){System.out.println(e.getMessage()); } saveXml(document, dir); } @Override public void addInfo(BasePojo basepojo) { BookInfo bi = (BookInfo)basepojo; Document document=null; SAXReader saxReader = new SAXReader(); try{ FileReader fr= new FileReader(new File(this.getDir())); document = saxReader.read(fr); }catch(Exception e){ e.printStackTrace(); } Element catalogElement = document.getRootElement(); Element book=catalogElement.addElement("book"); Element name=book.addElement("name"); addEleText(name,bi.getName()); Element title=book.addElement("title"); addEleText(title,bi.getTitle()); Element show = title.addAttribute("show", bi.getShow()); Element context = book.addElement("context"); addEleText(context,bi.getContext()); saveXml(document, this.getDir()); } public String getDir() { return dir; } public void setDir(String dir) { this.dir = dir; } @Override public void changeInfo(BasePojo basepojo) { } }
7、friendinfo的处理类
public class friendDomHander extends baseDomHander{ public friendDomHander(ModeDocumentFactory modeFactory) { super(modeFactory); } public void addInfo(FriendsInfo fi) { org.dom4j.Document document=null; org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader(); try{ FileReader fr= new FileReader(new File(getDir())); 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, getDir()); } public void changeInfo(BasePojo basepojo){ delInfo(basepojo); addInfo(basepojo); } @Override public void delInfo(BasePojo basepojo) { FriendsInfo fi = (FriendsInfo)basepojo; Document document=null; SAXReader saxReader = new SAXReader(); try{ FileReader fr= new FileReader(new File(getDir())); 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, getDir()); } @Override public List getinfo() { return null; } @Override public void createDocument( String dir) { setDir(dir); 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(dir))); output.write( document ); output.close(); } catch(IOException e){System.out.println(e.getMessage()); } saveXml(document, dir); } @Override public void addInfo(BasePojo basePojo) { delInfo( basePojo); FriendsInfo fi = (FriendsInfo)basePojo; Document document=null; SAXReader saxReader = new SAXReader(); try{ FileReader fr= new FileReader(new File(this.getDir())); 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, this.getDir()); } }
8、单例工厂
public class ModeDocumentFactory{ public static HashMap<String, baseDomHander> infoClassNameAndHandlerPool ; private static ResourceBundle resource; //默认xml路径 private static String DIR; //处理bean对象到xml的类 private static baseDomHander hander = null; static{ //将bean的classname作为key,处理bean的类作为value infoClassNameAndHandlerPool = new HashMap<String, baseDomHander>(); } public static ModeDocumentFactory instance = null; // private ThreadLocal perThreadCacheDir = new ThreadLocal(); private ModeDocumentFactory(){ try { init(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static ModeDocumentFactory getInstance(){ if(instance != null ){ return instance; }else{ instance = new ModeDocumentFactory(); } return instance; } private void init() throws InstantiationException, IllegalAccessException, ClassNotFoundException{ //获得配置文件对应的key -- value resource = ResourceBundle.getBundle("property"); Enumeration infoCN = resource.getKeys(); // Enumeration转换为Iterator 的对象适配器(这里纯粹是练习适配器模式) EnumerationAdapter enumerationAdapter= new EnumerationAdapter(infoCN); while( enumerationAdapter.hasNext() ) { String CN = (String) enumerationAdapter.next(); System.out.println(CN); String handerType = getHanderCN(CN); System.out.println(handerType); Class<?> classObject = Class.forName(handerType); //Class classObject = Thread.currentThread().getContextClassLoader().loadClass(handerType); Constructor<?> constructor = null; try { constructor = classObject.getConstructor(new Class[]{ModeDocumentFactory.class} ); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } try { //使用反射将value实例化,将ModeDocumentFactory传给实例 hander = (baseDomHander)constructor.newInstance(this); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //将处理对象放进内存里 infoClassNameAndHandlerPool.put(CN, hander ); } } public static String getDIR() { return DIR; } public String getHanderCN(String key){ return resource.getString(key); } public static baseDomHander createDocument(Class c,String dir){ baseDomHander bdHander = getHandlerFromPool( c); if(dir!=""&&dir!=null){ DIR=dir; } bdHander.createDocument(DIR); return bdHander; } public static baseDomHander createDocument(BasePojo basepojo ){ baseDomHander bdHander = getHandlerFromPool( basepojo.getClass()); bdHander.createDocument(DIR); return bdHander; } public static void addInfo(BasePojo basepojo ){ baseDomHander bdHander = getHandlerFromPool( basepojo.getClass()); bdHander.addInfo(basepojo); } public static void main(String[] args) { String filename = "d:/friends.xml"; String bookfilename = "d:/books.xml"; ModeDocumentFactory instance = ModeDocumentFactory.getInstance(); BookInfo finfo = creatbookinfo(); //createtmepinfo(); instance.createDocument(BookInfo.class,bookfilename); instance.addInfo(finfo); //instance.addInfo(finfo); //instance.delInfo(finfo); } public static baseDomHander getHandlerFromPool(Class c){ String key = c.getName(); String[] keys = key.split("\\."); String lastKey = keys[keys.length-1]; baseDomHander bdHander = (baseDomHander)infoClassNameAndHandlerPool.get(lastKey); return bdHander ; } public static void delInfo(BasePojo basepojo) { baseDomHander bdHander = getHandlerFromPool( basepojo.getClass()); bdHander.delInfo(basepojo); } public static void changeInfos(BasePojo basepojo) { baseDomHander bdHander = getHandlerFromPool( basepojo.getClass()); bdHander.changeInfo(basepojo); } public static List getinfo() { return null; } public static FriendsInfo createtmepinfo(){ FriendsInfo friendsinfo=new FriendsInfo("gao","lanyungang"); friendsinfo.setQQ("120316879"); friendsinfo.setImage("/images/text.png"); return friendsinfo; } public static BookInfo creatbookinfo(){ BookInfo bookInfo=new BookInfo(); bookInfo.setName("java"); bookInfo.setTitle("java_title"); bookInfo.setContext("内容"); bookInfo.setPrice("1000"); bookInfo.setShow("yes"); return bookInfo; } }
9.
BookInfo=com.ysen.dom02.bookDomHander FriendsInfo=com.ysen.dom02.friendDomHander
相关推荐
在项目中使用DOM4J时,只需将相应的jar包(如dom4j-1.6.1.jar、dom4j-2.0.2.jar或dom4j-2.1.1.jar)导入到类路径中,即可开始利用其功能处理XML文档。导入后,可以按照DOM4J提供的API进行编程,快速实现XML的读写...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了灵活、高效的API,使得XML的解析、创建、查询和修改变得更为简单。在本文中,我们将深入探讨DOM4J 1.6.1版本的安装及其在Maven项目中的应用。 首先,DOM4J...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了灵活且高效的API,使得XML的解析、创建、修改和查询变得简单易行。在Java世界中,DOM4J是与DOM、SAX和JDOM等其他XML处理库并驾齐驱的一个选择,尤其在处理...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了灵活、高效的API,使得XML的解析、创建、修改和查询变得简单。这次我们有两个版本的DOM4J库:1.6.1和2.0.0-ALPHA。这两个版本在功能、性能和API设计上都有所...
标题提及的"dom4j_1.6.1.jar"和"dom4j_2.1.0.jar"是两个不同版本的DOM4J库的Java档案文件,DOM4J是一个非常流行的Java XML API,用于处理XML文档。这两个版本的差异在于功能、性能优化和可能存在的bug修复。描述中...
另一份重要的资源是"dom4j-2.1.3-sources.jar",它提供了DOM4J库的源代码,允许开发者深入研究DOM4J的内部实现,对于理解DOM4J的工作原理、进行扩展或者调试都是不可或缺的。通过阅读源代码,开发者可以学习到如何...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了灵活且高效的API,使得XML的解析、创建、修改和查询变得简单。DOM4J 1.6是该库的一个较新版本,提供了完整的功能集以及对XML标准的广泛支持。 DOM4J的核心...
《深入解析DOM4J——基于Java的XML处理框架》 DOM4J,作为一个强大的Java XML API,是处理XML文档的主流工具之一。它提供了一套简单而高效的方式来操作XML文档,包括读取、写入、修改和遍历XML结构。在Java开发中,...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了简单而直观的API,使得开发者可以方便地读取、写入、修改以及操作XML文件。DOM4J 1.6.1是这个库的一个稳定版本,发布于较早时期,但依然广泛应用于许多Java...
标题提到的"所有版本的dom4j工具包"表明这是一个包含多版本DOM4J库的集合,可能包括了从早期到较新的一些版本,如dom4j-1.4.jar、dom4j-1.6.1.jar和dom4j-1.5.2.jar。这些不同版本的库可能分别对应于不同的功能特性...
赠送jar包:dom4j-2.1.1.jar; 赠送原API文档:dom4j-2.1.1-javadoc.jar; 赠送源代码:dom4j-2.1.1-sources.jar; 赠送Maven依赖信息文件:dom4j-2.1.1.pom; 包含翻译后的API文档:dom4j-2.1.1-javadoc-API文档-...
JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源代码 dom4j-1.6.1JavaEE源...
当我们遇到“java.lang.ClassNotFoundException: org.dom4j.DocumentException”的错误提示时,这通常意味着系统中缺少了DOM4j库,只需将“org.dom4j.jar”添加到项目类路径中即可解决此问题。 DOM4J是基于DOM和SAX...
赠送jar包:dom4j-2.1.1.jar; 赠送原API文档:dom4j-2.1.1-javadoc.jar; 赠送源代码:dom4j-2.1.1-sources.jar; 赠送Maven依赖信息文件:dom4j-2.1.1.pom; 包含翻译后的API文档:dom4j-2.1.1-javadoc-API文档-...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了灵活且高效的API,使得XML的解析、创建、修改和查询变得简单。DOM4J的名字来源于“Document Object Model for Java”,它采用面向对象的设计思想,提供了...
DOM4J是一个强大的Java库,专门用于处理XML文档。它提供了灵活、高性能的API,使得在Java应用程序中解析、创建、修改和操作XML文档变得容易。DOM4J因其易用性和丰富的功能集,在Java社区中广受欢迎,尤其在处理大型...
DOM4J 2.1.1 是一个针对Java平台的高效、开源的XML处理库,其全称为“Document Object Model for Java”。这个库提供了一系列强大的API,使得开发人员能够轻松地解析、创建、修改和操作XML文档。XML(eXtensible ...
DOM4J 的使用 DOM4J 是一个开源的 XML 解析包,由 dom4j.org 出品,应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM、SAX 和 JAXP。DOM4J 的主要特点是使用大量的接口,所有主要接口都在 org.dom4j 里面定义...
DOM4J是一个强大的Java库,专门用于处理XML文档。它是一个灵活且高性能的XML解析器,提供了丰富的API来创建、修改和操作XML数据。在Java应用程序中,DOM4J作为一个流行的选择,尤其在处理复杂的XML结构时,因为它...