Merge pull request #558 from leethomason/clone

Support clone (deep copy) of XMLDocument and XMLNode
This commit is contained in:
Lee Thomason
2017-06-15 12:01:48 -07:00
committed by GitHub
3 changed files with 131 additions and 2 deletions

View File

@@ -772,6 +772,18 @@ void XMLNode::SetValue( const char* str, bool staticMem )
}
}
XMLNode* XMLNode::DeepClone(XMLDocument* document) const
{
XMLNode* clone = this->ShallowClone(document);
if (!clone) return 0;
for (const XMLNode* child = this->FirstChild(); child; child = child->NextSibling()) {
XMLNode* childClone = child->DeepClone(document);
TIXMLASSERT(childClone);
clone->InsertEndChild(childClone);
}
return clone;
}
void XMLNode::DeleteChildren()
{
@@ -2038,6 +2050,19 @@ void XMLDocument::Clear()
}
void XMLDocument::DeepCopy(XMLDocument* target)
{
TIXMLASSERT(target);
if (target == this) {
return; // technically success - a no-op.
}
target->Clear();
for (const XMLNode* node = this->FirstChild(); node; node = node->NextSibling()) {
target->InsertEndChild(node->DeepClone(target));
}
}
XMLElement* XMLDocument::NewElement( const char* name )
{
XMLElement* ele = CreateUnlinkedNode<XMLElement>( _elementPool );