C++ tinyxml2::XMLNode::InsertEndChild()














































C++ tinyxml2::XMLNode::InsertEndChild()




InsertEndChild() adds a child node as the last (right) child. If the child node is already part of the document, it is moved from its old location to the new location. Returns the addThis argument or 0 if the node does not belong to the same document.



Syntax:-


XMLNode* tinyxml2::XMLNode::InsertEndChild(XMLNode *addThis)


Example:-


InsertEndChild_demo_file.xml:-


<book_info>

   

<book id="100">


        <name value="Tinyxml2"/>


        <edition number="2"/>

   

</book>


</book_info>


InsertEndChild.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("InsertEndChild_demo_file.xml");

  

cout << " Output : Using InsertEndChild() function of tinyxml2 " << endl;

  

tinyxml2::XMLElement* rootNode = doc.FirstChildElement();

   

tinyxml2::XMLElement* childNode = rootNode->FirstChildElement();

   

tinyxml2::XMLElement* newElement = doc.NewElement("Edition Year");


/*This will add New Element in Your XML file.*/

   

newElement->SetAttribute("Year : ", "2019");


/*This will set Attributes to Newly created Element in Your XML file.*/

   

childNode->InsertEndChild(newElement);

   

cout << "\n New End Child Element is Inserted Successfully in your XML File." << endl;

   

doc.SaveFile("InsertEndChild_demo_file.xml");


return 0;


}


Output :-




After this Tinyxml2 InsertEndChild.exe program runs successfully.  We can see that End Child

Element is inserted with name Edition Year with attribute year.

A file " InsertEndChild_demo_file.xml " is updated as follows using savefile()


InsertEndChild_demo_file.xml  (After execution of InsertEndChild.exe file)

<book_info>

   

<book id="100">

       

<name value="Tinyxml2"/>

       

<edition number="2"/>

       

<Edition Year Year : = "2019"/ >

   

</book>


</book_info>


Comments