C++ tinyxml2::XMLDocument::NewText()














































C++ tinyxml2::XMLDocument::NewText()



NewText() creates a new Text associated with the Document. The memory for the Text is managed by the Document.

Syntax:-
XMLText* tinyxml2::XMLDocument::NewText(const char * text)

Example:-

NewText_demo_file.xml

<?xml version="1.0"?>


<ParentNode>This_is_Parent_Node_Content.</ParentNode>


NewText.cpp (Filename)

/*include necessary header files*/

#include <iostream>

#include <cstdlib>

#include "tinyxml2.h"

using namespace std;

using namespace tinyxml2;

int main (int argc, char * const argv[])

{  

XMLDocument doc;  


doc.LoadFile("NewText_demo_file.xml");


 XMLElement*tag = doc.NewElement("New_Tag");     


/*This will create new tag with name <New_Tag> in your file*/      

                

 XMLText*tag_content =doc.NewText("This_is_New_Tag_Content_Newly_Inserted.");

   

/*This will add content to your Newly created tag*/


 tag->LinkEndChild(tag_content);    


 doc.LinkEndChild(tag);


 doc.SaveFile("NewText_demo_file.xml");


/*update changes to your original file*/


printf("Output: \n NewText_demo_file.xml is Updated Successfully.");


}


Output:-

        NewText_demo_file.xml is Updated Successfully. 




After this Tinyxml2  NewText.cpp program runs successfully. 

A file named  "NewText_demo_file.xml" is updated and saved in current Working Directory.


NewText_demo_file.xml file contents is as follows:

<?xml version="1.0"?>


<ParentNode>This_is_Parent_Node_Content.</ParentNode>


<New_Tag>This_is_New_Tag_Content_Newly_Inserted.</New_Tag>


Comments