C++ tinyxml2::SetText()

C++ tinyxml2::SetText()

SetText() function is used to easily access the text inside an element. Although easy and concise, SetText() is limited compared to creating an XMLText child and mutating it directly. If the first child of 'this' is an XMLText, SetText() sets its value to the given string, otherwise it will create a first child that is an XMLText.


void tinyxml2::XMLElement::SetText(const char * inText)




<title>Tinyxml2 Developer's Guide</title>






SetText.cpp (Filename):-

/*include necessary header files*/

#include <iostream>

#include <cstdlib>

#include "tinyxml2.h"

using namespace std;

using namespace tinyxml2;

int main()



tinyxml2::XMLDocument doc;



cout << "\n Output : Using SetText() function of tinyxml2 " << endl;


XMLNode* book_info = doc.FirstChildElement();


XMLNode* book1 = book_info->FirstChildElement();


XMLElement* bk1 = book1->ToElement();


bk1->SetText("This is newly updated text of title");

/*This will set name of "name" child as "title".*/


cout << "\t New Text of Title tag after SetText() function is applied Successfully." << endl;




return 0;



After this Tinyxml2  SetText.cpp  program runs successfully. SetText_demo_file.xml

file contents are as follows:

SetText_demo_file.xml :-



<title>This is newly updated text of title</title>