Dom4J两种节点添加方法比较
Dom4J中,給一個已存在的節(jié)點添加子節(jié)點的方法有兩種:
通過DocumentFactory得到Element然后通過父節(jié)點的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();
??? }
?
?
兩種方法都是非常經(jīng)典的方法,但是執(zhí)行DocumentTest()方法,會出現(xiàn)org.dom4j.IllegalAddException 異常,要解決這個異常,也很容易,我們可以使用類Element的clone()方法(繼承自Object類)得到該Element的一個副本,副本的含義,是:
要同時使對于任何對象 x,表達(dá)式:
x.clone() != x
為 true,表達(dá)式:
x.clone().getClass() == x.getClass()
也為 true,但這些并非必須要滿足的要求。一般情況下:
x.clone().equals(x)
為 true,但這并非必須要滿足的要求。
成立。
Dom4j 中,在給一個元素添加
?
所有,就業(yè)務(wù)需要來說,用兩種方式都是可以的,但是,他們的執(zhí)行效率一樣嗎?
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("方法一執(zhí)行時間"+(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("方法二執(zhí)行時間"+(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("節(jié)點大小:"+index);
??????????? DocumentTest();
??????????? DocumentTest2();
??????????? //double per=DocumentTest()/DocumentTest2();
??????????? //System.out.println("時間對比:"+per);
??????????? ;
??? ??????? //DocumentTest2();
??????? }
???????
???????
??? }
我們通過上述代碼來檢查一下執(zhí)行時間,運(yùn)行結(jié)果如下:
?
節(jié)點大小:10
方法一執(zhí)行時間33ms
方法二執(zhí)行時間0ms
節(jié)點大小:100
方法一執(zhí)行時間0ms
方法二執(zhí)行時間0ms
節(jié)點大小:1000
方法一執(zhí)行時間0ms
方法二執(zhí)行時間0ms
節(jié)點大小:10000
方法一執(zhí)行時間15ms
方法二執(zhí)行時間63ms
節(jié)點大小:100000
方法一執(zhí)行時間265ms
方法二執(zhí)行時間327ms
?
?
兩個方法的內(nèi)存開銷并沒用本質(zhì)區(qū)別,都需要創(chuàng)建相應(yīng)數(shù)量的對象,但是,在節(jié)點數(shù)較少的情況下,時間開銷相差非常可觀,在節(jié)點數(shù)比較多的情況下,方法一時間開銷也始終優(yōu)于方法二。
轉(zhuǎn)載于:https://www.cnblogs.com/MicroGoogle/archive/2011/12/13/2286616.html
總結(jié)
以上是生活随笔為你收集整理的Dom4J两种节点添加方法比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: login控件设置居中
- 下一篇: 7个你不知道的WP7开发工具