日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Dom4J两种节点添加方法比较

發布時間:2025/3/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

?

?

兩個方法的內存開銷并沒用本質區別,都需要創建相應數量的對象,但是,在節點數較少的情況下,時間開銷相差非常可觀,在節點數比較多的情況下,方法一時間開銷也始終優于方法二。

轉載于:https://www.cnblogs.com/MicroGoogle/archive/2011/12/13/2286616.html

總結

以上是生活随笔為你收集整理的Dom4J两种节点添加方法比较的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。