C++ tinyxml2::XMLDocument::NewElement()

C++ tinyxml2::XMLDocument::NewElement()

NewElement() creates a new Element associated with this Document. The memory for the Element is managed by the Document.

Here using NewElement() we have created new xml element associated with the Document but new tag will look like this <New_Tag_1/> instead of  <New_Tag1> </New_Tag_1>.This is Because we have not inserted any data into our NewElement(). Also <New_Tag_1/> is equivalent to  <New_Tag1> ....</New_Tag_1>


XMLElement* tinyxml2::XMLDocument::NewElement(const char * name)



<?xml version="1.0"?>


NewElement.cpp (Filename)

#include <iostream>

#include <cstdlib>

#include "tinyxml2.h"

using namespace std;

using namespace tinyxml2;

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


XMLDocument doc; 


  XMLElement *tag_1 = doc.NewElement("New_Tag_1");     

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



  XMLElement *tag_2 = doc.NewElement("New_Tag_2");     

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




/*update changes to your original file*/

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


Output :-

        NewElement_demo_file.xml is Updated Successfully. 

After this Tinyxml2 NewElement.cpp program runs successfully. 

A file named NewElement_demo_file.xml is updated and saved in the Current Working Directory.

NewElement_demo_file.xml file contents are as follows:

<?xml version="1.0"?>