c++ - Deep Copy an XML via TinyXML -
i using tinyxml.
how duplicate or create copy of existing xmldocument?
http://www.grinninglizard.com/tinyxmldocs/classtixmldocument.html#a4e8c1498a76dcde7191c683e1220882
i went through link says using clone replicate node. protected , not want go deriving class out of , like.
i not want save existing xmldocument file , making xmldocument object read file have copy of it.
i not able perform deep copy using memcpy because unaware of size of entire xml.
i not want having 2 objects being used 1 after other like:
xmldocumentobj1 = add_some_data xmldocumentobj2 = add_the_same_data, , on
the primary reason want second copy that, first might modified different sections of code, while same copy being 'read' @ multiple places. need ensure there occur no errors when xmldocument read, because there chances might have been modified in background running thread, , no program crashes.
here wrote deep copy. takes source node , copies under destination node, children , all. memory taken destination node's context. hopefully, it's start in right direction you.
void copynode(tinyxml2::xmlnode *p_dest_parent, const tinyxml2::xmlnode *p_src) { // protect evil if (p_dest_parent == null || p_src == null) { return; } // document context new memory allocated tinyxml2::xmldocument *p_doc = p_dest_parent->getdocument(); // make copy tinyxml2::xmlnode *p_copy = p_src->shallowclone(p_doc); if (p_copy == null) { // error handling required (e.g. throw) return; } // add child p_dest_parent->insertendchild(p_copy); // add grandkids (const tinyxml2::xmlnode *p_node = p_src->firstchild(); p_node != null; p_node = p_node->nextsibling()) { copynode(p_copy, p_node); } }
Comments
Post a Comment