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

有帮助吗?

解决方案

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

  

DOMDocument :: createXXXX创建的对象   用户可以调用release()函数来指示任何孤立节点的释放。释放孤立节点时,其关联的子节点也将被释放。访问已发布的节点将导致意外行为。这些孤立节点最终将被释放,如果尚未这样做,则在其所有者文档发布时

我已经解决了这种情况,保留了一个便笺簿 DOMDocument并使用它来创建片段或孤立节点,并在我准备好时将它们应用到目标文档中。 E.g。

// 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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top