C++ tinyxml2::XMLElement::SetAttribute()














































C++ tinyxml2::XMLElement::SetAttribute()



      

SetAttribute() of Tinyxml2 Element is used to set an attribute of name to a given value.


Syntax:-

 void SetAttribute(int value)
void SetAttribute(double value)
void SetAttribute(float value)

Example:-


SetAttribute_demo_file.xml:-


<book_info>


<book id="100" name="Tinyxml2" edition="2" publish_year="2020">

       

<title>Article</title>


</book>


</book_info>


SetAttribute.cpp (Filename):-

/*include necessary header files*/


#include <iostream>


#include <cstdlib>


#include "tinyxml2.h"


using namespace std;

using namespace tinyxml2;


int main()


{

   

XMLDocument xmlDocument;

   

xmlDocument.LoadFile("SetAttribute_demo_file.xml");


cout << "\n Output : Using SetAttribute() function of tinyxml2 " << endl;

   

XMLNode* book_info = xmlDocument.FirstChildElement();

   

XMLNode* book1 = book_info->FirstChildElement();

   

XMLElement* bk1 = book1->ToElement();

   

bk1->ToElement()->SetAttribute("id", 10001);


/*This will set "id" Attribute of Book as 10001*/

   

cout << "\n Id Attribute of Book Element is Successfully updated." << endl;


xmlDocument.SaveFile("SetAttribute_demo_file.xml");


return 0;


}



Output:-


               

                  

After this Tinyxml2  SetAttribute.exe program runs successfully. 

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


SetAttribute_demo_file.xml  (After execution of SetAttribute.exe file)-

<book_info>


<book id="10001" name="Tinyxml2" edition="2" publish_year="2020">

       

<title>Article</title>


</book>


</book_info>



Comments