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.



Syntax:-


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


Example:-


SetText_demo_file.xml:-


<Book_Info>

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

   

<genre>Computer</genre>

   

<publish_date>1999-01-26</publish_date>

</Book_Info>


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;

doc.LoadFile("SetText_demo_file.xml");

   

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;

   

doc.SaveFile("SetText_demo_file.xml");

  

return 0;

}


Output:-


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

file contents are as follows:


SetText_demo_file.xml :-

<Book_Info>

   

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

   

<genre>Computer</genre>

   

<publish_date>1999-01-26</publish_date>

</Book_Info>


Comments