C++ tinyxml2::XMLDocument::SaveFile()














































C++ tinyxml2::XMLDocument::SaveFile()




SaveFile() of tinyxml2 writes the document represented by a tinyxml2::XMLDocument to a file.

SaveFile() saves the XML file to the disk. Returns XML_SUCCESS (0) on success, or an errorID.


Syntax:-

XMLError tinyxml2::XMLDocument::SaveFile(const char* filename,bool compact=false)

Example:-

SaveFile.cpp (Filename)

#include <iostream>

#include <cstdlib>

#include "tinyxml2.h"

using namespace std;


int main()

{

tinyxml2::XMLDocument xmldocument;

tinyxml2::XMLNode * root_node = xmldocument.NewElement("Root Node");

xmldocument.InsertFirstChild(root_node);

tinyxml2::XMLElement * root_element_1 = xmldocument.NewElement("Child Node");

root_element_1->SetText("This_is_Child_node_element");

root_node->InsertEndChild(root_element_1);

xmldocument.SaveFile("NewFile.xml" );

/*This will create new xml file of name NewFile.xml in your Current working Directory.*/

printf("Output : \n\tNewFile.xml is created Successfully.");


return 0;

}

Output :-   

NewFile.xml is created Successfully.


     


After this Tinyxml2  SaveFile.cpp program runs successfully. A new file named "NewFile.xml" is created and saved in current Working Directory.


NewFile.xml file contents is as follows:

NewFile.xml :-

<Root Node>

<Child Node>This_is_Child_node_element</Child Node>

</Root Node>


Comments