Reputation: 313
In TinyXmlv1 i can create a temp Xml Element then Parse document by
TiXmlDocument doc;
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.Parse("<TAGS></TAGS>"); // It OK
Now i want switch to TinyXmlv2 by following:
#include "tinyxml2.h"
using namespace tinyxml2;
int main(int argc, char* argv[])
{
tinyxml2::XMLDocument doc;
tinyxml2::XMLElement* newElm = doc.NewElement("Hello");
newElm->SetText("World");
doc.Parse("<TAGS></TAGS>"); // This will crash
return 0;
}
I cant understand why it crash.
Upvotes: 0
Views: 821
Reputation: 1181
It's not a "crash" but an assert
from tinyxml2 because you are "throwing away" newElem
. You created newElem
within XMLDocument
doc
but newElem
is just "floating" around as an untracked node until you insert it at a specific location within the XMLDocument
. Calling Parse
clears the XMLDocument
deleting all current nodes and the assert
is simply a notification that an untracked node is being deleted.
Call one of the XMLNode::Insert...
methods to add elements to the document in the appropriate place. And, in your case, move the call to Parse
to create the document element (<TAGS>
) before creating child elements.
E.g.
#include "tinyxml2.h"
using namespace tinyxml2;
int main(int argc, char* argv[])
{
XMLDocument doc;
doc.Parse("<TAGS></TAGS>");
XMLElement* newElm = doc.NewElement("Hello");
newElm->SetText("World");
doc.InsertEndChild(newElem);
return 0;
}
My tinyxml2 extension offers a convenient helper function (append_element
) to create and insert an element in a single operation.
Upvotes: 0