xerces_3_1 approveNode()方法返回NULL
xerces_3_1 adoptNode() method returns NULL
我目前在visualstudio2010中使用xerces3.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(...)
问题!
xerces-api为adoptNode(DOMNode* source)
声明以下内容:
更改节点的ownerDocument、其子节点以及附加的属性节点(如果有的话)。
经过一些研究,我查看了xerces3.1中adoptNode的实现,可悲的事实是这是不可能的。引用源代码:
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变通方法)
相关文章:
- 从父类方法返回子类对象
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 让bool方法返回其他整数
- 不能将方法返回的值用于另一个方法
- 从私有成员变量的成员方法返回unique_ptr
- 我的模板类方法返回错误类型?
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 我无法使用C++指针指向类方法返回的 std::vector
- 对象引用中的字段以不同的方法返回不同的值
- 类方法返回指向具有模板的类成员的指针
- 如何将字符串从 C++/CLI 方法返回到调用它的非托管C++
- 从工厂方法返回的ComPtr的引用计数增加两次
- 如何使用 SFINAE 从 end() 方法返回 (const_) 迭代器
- Arduino trim() 和 replace() 方法返回从 'void' 到非标量类型'String'请求的转换
- 从类方法返回 "const char*" 作为 std::string&
- 如何使用 "get" 方法返回类的私有 std::unique_ptr 成员
- 可能从单个方法返回不同类型的对象
- qml 未知方法返回类型:ArchiveFile*,即使调用了 qmlRegisterUncreatableType
- 如何从方法返回静态常量 int std::array?
- 当从其他方法返回 vector 时,C++无法访问矢量元素