tags:

views:

326

answers:

1

I am using Xerces-c in my project, and would like to create a single DOMElement without having to create a whole new DOMDocument. Is such a thing possible?

A: 

I haven't seen a way. AFAIK the DOMDocument acts as the "memory pool" and all elements are created in this pool. In the Xerces docs we see:

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

I've worked around this situation by keeping a scratch pad DOMDocument around and using it to create fragments or orphan nodes and adopting them into their destination documents when I'm ready. 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);
maccullt