deepclone

TM
2013-01-19
2013-05-20
  • TM
    TM
    2013-01-19

    Deep Clone:  I see there is an 'XMLNode::ShallowClone(doc)'.
    Am I missing something? I will write a 'DeepClone() and post it here, but do I need to write that? I would think it'd be somewhere. Thanks!

     
  • TM
    TM
    2013-01-24

    // DeepClone All children of 'nodeSrc' into 'newNode', using document 'DocDest', and nodeSrc's parent is 'parent'.
    // Nice recursive function. Code by: Todd A. Marshall
    void CMyClass::DeepClone(tinyxml2::XMLNode *newNode,tinyxml2::XMLNode *nodeSrc,tinyxml2::XMLDocument *DocDest,tinyxml2::XMLNode *parent)
    {
    tinyxml2::XMLNode *child = nodeSrc->FirstChild(); // Start at first child
    tinyxml2::XMLNode *newNode2;
    if (child)
    {
    newNode2 = child->ShallowClone(DocDest); // Copy child to newNode2
    DeepClone(newNode2,child,DocDest,nodeSrc); // Deep clone the child into newNode2 children
    newNode->InsertFirstChild(newNode2); // Insert the child created (newNode2)
    }
    else return; // NO CHILDREN: no first child, done.
    tinyxml2::XMLNode *child2; // Next source child cloned
    tinyxml2::XMLNode *lastClone = newNode2; // last child created
    while (1) // deep clone children 2..n
    {
    child2 = child->NextSibling();
    if (!child2)
    break;
    tinyxml2::XMLNode *newNode2 = child2->ShallowClone(DocDest); // Copy child
    DeepClone(newNode2,child2,DocDest,nodeSrc); // Deep clone the child
    newNode->InsertAfterChild(lastClone,newNode2);
    child = child2;
    lastClone = newNode2;
    }
    }

     
  • Roberto Lopez
    Roberto Lopez
    2013-02-07

    Nice code!

    A couple of constants could be added here:

    void DeepClone(tinyxml2::XMLNode*, const tinyxml2::XMLNode*, tinyxml2::XMLDocument*, const tinyxml2::XMLNode*);

    Also, I agree with you that deep clone should be included in tinyxml2.