Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text in tinyxml2

Tags:

c++

xml

tinyxml2

I am trying to create an XML file using TinyXML2. While there are many examples and tutorials on loading, there seems to be very little on saving.

I basically want to end up with:

<node>text</node>

I know I can get the "node" parts by adding an element, but how do I set the text part? Element has a GetText but I can find no SetText. There is also an XMLText class, but that has no methods for setting the text!

like image 917
Simon Parker Avatar asked Nov 02 '12 00:11

Simon Parker


1 Answers

I built with G++ 4.2.1 on OS X 10.6, and it runs fine. Output is <Version>1.0.0</Version>.

#include "tinyxml2.h"

using namespace tinyxml2;

int main(int argc, char* argv[])
{
   tinyxml2::XMLDocument doc;
   tinyxml2::XMLElement* versionNode = doc.NewElement("Version");
   tinyxml2::XMLText* versionText = doc.NewText("1.0.0");

   versionNode->InsertEndChild(versionText);
   doc.InsertEndChild(versionNode);

   XMLPrinter printer;
   doc.Print();

   return 0;
}
like image 54
Jason Avatar answered Sep 23 '22 05:09

Jason