c++xmltinyxml2

Adding text in 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!


Solution

  • 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;
    }