C++ tinyxml2::Accept()

C++ tinyxml2::Accept()


Accept() is a method which is used to carry out  a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the XML tree will be conditionally visited and the host will be called back via the XMLVisitor interface.

This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this interface versus any other.)


virtual bool tinyxml2::XMLNode::Accept(XMLVisitor *visitor) const


#include "tinyxml2.h"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace tinyxml2;
using namespace std;
bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
bool pass;
if ( !expected && !found )
pass = true;
else if ( !expected || !found )
pass = false;
pass = !strcmp( expected, found );
if ( !echo ) {
printf (" %s\n", testString);
else {
if ( extraNL ) {
printf( " %s\n", testString );
printf( "%s\n", expected );
printf( "%s\n", found );
else {
printf (" %s [%s][%s]\n", testString, expected, found);
bool XMLTest(const char* testString, XMLError expected, XMLError found, bool echo = true, bool extraNL = false)
 return XMLTest(testString, XMLDocument::ErrorIDToName(expected), XMLDocument::ErrorIDToName(found), echo, extraNL);
template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
bool pass = ( expected == found );
if ( !echo )
printf (" %s\n", testString);
else {
char expectedAsString[64];
XMLUtil::ToStr(expected, expectedAsString, sizeof(expectedAsString));
char foundAsString[64];
XMLUtil::ToStr(found, foundAsString, sizeof(foundAsString));
printf (" %s [%s][%s]\n", testString, expectedAsString, foundAsString );
int main( int argc, const char ** argv )
const char* passages =
"<?xml version=\"1.0\" standalone=\"no\" ?>"
"<str context=\"This Data will be written to the Test file you have selected."
" This is done using Accept() function and writing data in specific file.\"> </str>"
XMLDocument doc;
doc.Parse( passages );
XMLElement* str = doc.RootElement()->FirstChildElement();
const char* textFilePath = "textfile.txt";
FILE* textfile = fopen( textFilePath, "w" );
if ( textfile )
XMLPrinter streamer( textfile );
bool acceptResult = str->Accept( &streamer );
fclose( textfile );
XMLTest( "Output of Entity transformation : Accept", true, acceptResult );
return 0;

Textfile.txt(before accept.cpp code is executed)

This is text file before execution of Code

Textfile.txt(after accept.cpp  code is executed)

<str context="This Data will be written to the Test file you have selected. This is done using Accept() function and writing data in specific file."/>