2015-11-03 106 views
1

我目前與2010年xerces_3_1 adoptNode()方法返回NULL

我寫這個(很簡單)的代碼在Visual Studio 3.1的Xerces工作:

XMLPlatformUtils::Initialize(); 
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(L"XML 1.0"); 

DOMDocument* doc1 = impl->createDocument(L"nsURI", L"abc:root1", 0); 
DOMDocument* doc2 = impl->createDocument(0, L"root2", 0); 
DOMElement* root1 = doc1->getDocumentElement(); 
DOMElement* root2 = doc2->getDocumentElement(); 

DOMElement* el1 = doc1->createElement(L"el1"); 
root1->appendChild(el1); 

DOMNode* tmpNode = doc2->adoptNode(el1); //tmpNode is null after this line 
root2->appendChild(tmpNode); 

doc1->release(); 
doc2->release(); 
xercesc::XMLPlatformUtils::Terminate(); 

的問題是,adoptNode(...)方法將總是返回一個空指針,無論如何。我真的不明白這裏發生了什麼,請幫助我!

PS:我知道我可以使用importNode(...)方法和刪除,並從舊文件釋放舊的節點,但我希望能有辦法解決我的問題adoptNode(...)

回答

0

了Xerces API國adoptNode(DOMNode* source)如下:

更改一個節點,其子女的ownerDocument,以及附加的屬性節點,如果有任何。

經過一番研究,我看到了xerces 3.1中採納節點的實現,可悲的是,這是不可能的。引用的源代碼:

if(sourceNode->getOwnerDocument()!=this) 
{ 
    // cannot take ownership of a node created by another document, as it comes from its memory pool 
    // and would be delete when the original document is deleted 
    return 0; 
} 

編輯

有這個方法的方法,但它需要DOM-實施的一些知識(使用的UserData尤其是當)。您可以使用importNode(...)導入節點,並從舊文檔中刪除另一個節點。

應該釋放舊節點以避免浪費內存!

如果您已將用戶數據附加到舊節點,那麼新文檔必須有一些UserDataHandler,它採用從舊節點到新節點的用戶數據!

請注意,舊節點上的可能引用現在不指向新節點。他們必須手動更改(或使用一些UserDataHandler解決方法)