gpt4 book ai didi

c++ - 如何创建一个空的 DOMElement

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:01:41 26 4
gpt4 key购买 nike

我在我的项目中使用 Xerces-c,并且想创建一个 DOMElement 而不必创建一个全新的 DOMDocument。这样的事情可能吗?

最佳答案

我还没有看到方法。 AFAIK DOMDocument 充当“内存池”,所有元素都在该池中创建。在 Xerces docs我们看到:

Objects created by DOMDocument::createXXXX Users can call the release() function to indicate the release of any orphaned nodes. When an orphaned Node is released, its associated children will also be released. Access to a released Node will lead to unexpected behaviour. These orphaned Nodes will eventually be released, if not already done so, when its owner document is released

我通过保留一个便签本 DOMDocument 并使用它来创建片段或孤立节点并在我准备好时将它们采用到它们的目标文档中来解决这种情况。例如

// 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);

关于c++ - 如何创建一个空的 DOMElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/223314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com