2008-10-21 86 views

回答

2

我還沒有看到一種方法。 AFAIK DOMDocument充當「內存池」,所有元素都在這個池中創建。在Xerces的docs我們看到:通過DOM文檔:: createXXXX 用戶創建可以調用釋放()函數來表示所有孤立節點釋放

對象。當一個孤兒節點被釋放時,其相關的孩子也將被釋放。訪問已發佈的節點將導致意外的行爲。這些孤立節點最終會被釋放,如果不這樣做的話,它的主人文件發佈

我已經保持便箋圍繞DOM文檔,並使用它來創建片段或孤兒解決這種情況下工作時,節點並在我準備好時將它們採用到目標文檔中。例如。

// Create a fragment holding two sibling elements. The first element also has a child. 
DOMDocumentFragment* frag = scratchDom->createDocumentFragment(); 
DOMNode* e1 = frag->appendChild(frag->getOwnerDocument()->createElement("e1")); 
e1->appendChild(e1->getOwnerDocument()->createElement("e1-1")); 
DOMNode* e2 = frag->appendChild(frag->getOwnerDocument()->createElement("e2")); 
... 
// Paste the contents of the fragment into a "parent" node from another document 
DOMNode* parentFromOtherDom = ...; 
parentFromOtherDom->appendChild(parentFromOtherDom->getOwnerDocument()->adopt(frag)); 
scratchDom->removeChild(frag);