zoukankan      html  css  js  c++  java
  • Dom4J两种节点添加方法比较

    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

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

  • 相关阅读:
    MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论
    创建,增加,删除mysql表分区
    mysql分区及实例演示
    MySQL的表分区详解
    MySQL 存储过程传参之in, out, inout 参数用法
    MySQL里面的子查询实例
    超详细mysql left join,right join,inner join用法分析
    MySQL force Index 强制索引概述
    MyISAM和InnoDB的索引在实现上的不同
    java中的守护线程
  • 原文地址:https://www.cnblogs.com/MicroGoogle/p/2286616.html
Copyright © 2011-2022 走看看