Dom4J两种节点添加方法比较

来源:互联网 发布:期货数据下载 编辑:程序博客网 时间:2024/06/11 09:06

Dom4J中,给一个已存在的节点添加子节点的方法有两种:

通过DocumentFactory得到Element然后通过父节点的add(Element elem)方法添加,

通过Element ielem= Element.addElement(StringQName);方法来添加:

 


 

public staticvoidDocumentTest(){        org.dom4j.DocumentFactoryDocumentFactory = new org.dom4j.DocumentFactory();               org.dom4j.Elementroot = DocumentFactory.createElement("Books");        Elementbook=DocumentFactory.createElement("Book");        book.setText("The Road Ahead");        for(int i=0;i<10;i++){            book.addAttribute("ISBN", "ITCP:0WESAS"+i);            root.add(book);            //root.add((Element)book.clone());        }        System.out.println(root.asXML());    }    public static void DocumentTest2(){        org.dom4j.DocumentFactoryDocumentFactory = new org.dom4j.DocumentFactory();               org.dom4j.Elementroot = DocumentFactory.createElement("Books");        for(int i=0;i<10;i++){            Elementbook=null;            book=root.addElement("book");            book.setText("The Road Ahead");            book.addAttribute("ISBN", "ITCP:0WESAS"+i);            //root.add(book);        }        System.out.println(root.asXML());    }    public static void main(String[] args){        DocumentTest();    } 

两种方法都是非常经典的方法,但是执行DocumentTest()方法,会出现org.dom4j.IllegalAddException异常,要解决这个异常,也很容易,我们可以使用类Element的clone()方法(继承自Object类)得到该Element的一个副本,副本的含义,是:

要同时使对于任何对象 x,表达式:

x.clone() != x

为 true,表达式:

x.clone().getClass() == x.getClass()

也为 true,但这些并非必须要满足的要求。一般情况下:

x.clone().equals(x)

为 true,但这并非必须要满足的要求。

成立。

Dom4j 中,在给一个元素添加

 

所有,就业务需要来说,用两种方式都是可以的,但是,他们的执行效率一样吗?

public staticintindex=10;    public static long DocumentTest(){        //DefaultElement df=new DefaultElement();        java.util.Datetime1=newjava.util.Date();        org.dom4j.DocumentFactoryDocumentFactory = new org.dom4j.DocumentFactory();               org.dom4j.Elementroot = DocumentFactory.createElement("Books");        Elementbook=DocumentFactory.createElement("Book");        book.setText("The Road Ahead");        for(int i=0;i<index;i++){            book.addAttribute("ISBN", "ITCP:0WESAS"+i);            root.add((Element)book.clone());        }        java.util.Datetime2=newjava.util.Date();        System.out.println("方法一执行时间"+(time2.getTime()-time1.getTime())+"ms");        returntime2.getTime()-time1.getTime();        //System.out.println(root.asXML());    }     public static long DocumentTest2(){        org.dom4j.DocumentFactoryDocumentFactory = new org.dom4j.DocumentFactory();        java.util.Datetime1=newjava.util.Date();        org.dom4j.Elementroot = DocumentFactory.createElement("Books");        for(int i=0;i<index;i++){            Elementbook=null;            book=root.addElement("book");            book.setText("The Road Ahead");            book.addAttribute("ISBN", "ITCP:0WESAS"+i);            //root.add(book);        }        java.util.Datetime2=newjava.util.Date();               System.out.println("方法二执行时间"+(time2.getTime()-time1.getTime())+"ms");        returntime2.getTime()-time1.getTime();         //System.out.println(root.asXML());    }    public static void main(String[] args){        index=10;        for(index=10;index<=100000;index=index*10){            System.out.println("节点大小:"+index);            DocumentTest();            DocumentTest2();            //double per=DocumentTest()/DocumentTest2();            //System.out.println("时间对比:"+per);            ;            //DocumentTest2();        }                  }


我们通过上述代码来检查一下执行时间,运行结果如下:

 

节点大小:10

方法一执行时间33ms

方法二执行时间0ms

节点大小:100

方法一执行时间0ms

方法二执行时间0ms

节点大小:1000

方法一执行时间0ms

方法二执行时间0ms

节点大小:10000

方法一执行时间15ms

方法二执行时间63ms

节点大小:100000

方法一执行时间265ms

方法二执行时间327ms

 

 

两个方法的内存开销并没用本质区别,都需要创建相应数量的对象,但是,在节点数较少的情况下,时间开销相差非常可观,在节点数比较多的情况下,方法一时间开销也始终优于方法二。


原创粉丝点击