代码改变世界

Dom4J两种节点添加方法比较

2011-12-13 18:02  jinze  阅读(4427)  评论(0编辑  收藏  举报

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

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

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

 

public static void DocumentTest(){

        org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory();

       

        org.dom4j.Element root = DocumentFactory.createElement("Books");

        Element book=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.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory();

       

        org.dom4j.Element root = DocumentFactory.createElement("Books");

        for(int i=0;i<10;i++){

            Element book=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 异常,要解决这个异常,也很容易,我们可以使用类Elementclone()方法(继承自Object类)得到该Element的一个副本,副本的含义,是:

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

x.clone() != x

为 true,表达式:

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

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

x.clone().equals(x)

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

成立。

Dom4j 中,在给一个元素添加

 

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

public static int index=10;

    public static long DocumentTest(){

        //DefaultElement df=new DefaultElement();

        java.util.Date time1=new java.util.Date();

        org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory();

       

        org.dom4j.Element root = DocumentFactory.createElement("Books");

        Element book=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.Date time2=new java.util.Date();

        System.out.println("方法一执行时间"+(time2.getTime()-time1.getTime())+"ms");

        return time2.getTime()-time1.getTime();

        //System.out.println(root.asXML());

    }

 

    public static long DocumentTest2(){

        org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory();

        java.util.Date time1=new java.util.Date();

        org.dom4j.Element root = DocumentFactory.createElement("Books");

        for(int i=0;i<index;i++){

            Element book=null;

            book=root.addElement("book");

            book.setText("The Road Ahead");

            book.addAttribute("ISBN", "ITCP:0WESAS"+i);

            //root.add(book);

        }

        java.util.Date time2=new java.util.Date();

       

        System.out.println("方法二执行时间"+(time2.getTime()-time1.getTime())+"ms");

        return time2.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

 

 

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

用电子邮件联系Andy