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.
It's not a "crash" but an
assertfrom tinyxml2 because you are "throwing away"newElem. You creatednewElemwithinXMLDocumentdocbutnewElemis just "floating" around as an untracked node until you insert it at a specific location within theXMLDocument. CallingParseclears theXMLDocumentdeleting all current nodes and theassertis 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 toParseto create the document element (<TAGS>) before creating child elements.E.g.
My tinyxml2 extension offers a convenient helper function (
append_element) to create and insert an element in a single operation.