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 異常,要解決這個異常,也很容易,我們可以使用類Element的clone()方法(繼承自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两种节点添加方法比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: login控件设置居中
- 下一篇: error C1189: #error