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>


Syntax:-

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


Example:-

NewElement_demo_file.xml

<?xml version="1.0"?>


<ParentNode>This_is_Parent_Node_Content.</ParentNode>

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; 


doc.LoadFile("NewElement_demo_file.xml");


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


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

      

  doc.LinkEndChild(tag_1);


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


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

        

  doc.LinkEndChild(tag_2);


doc.SaveFile("NewElement_demo_file.xml");


/*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"?>


<ParentNode>This_is_Parent_Node_Content.</ParentNode>

<New_Tag_1/>

<New_Tag_2/>


Comments