【问题标题】:xerces_3_1 adoptNode() method returns NULLxerces_3_1 采用Node() 方法返回NULL
【发布时间】:2016-02-03 07:22:09
【问题描述】:

我目前在 Visual Studio 2010 中使用 xerces 3.1。

我写了这段(非常简单的)代码:

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(...) 的问题!

【问题讨论】:

    标签: c++ dom xerces ownership xerces-c


    【解决方案1】:

    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 解决方法)

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 2023-03-05
      相关资源
      最近更新 更多