Click here to Skip to main content
15,891,248 members
Articles / Programming Languages / C++

A simple STL based XML parser

Rate me:
Please Sign up or sign in to vote.
4.21/5 (22 votes)
16 May 2000 253.3K   4.6K   70  
This is a small non-validating XML parser based purely on STL
// XmlParser.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define DebugDisplay

#include "XmlStream.h"


#include <conio.h>
#include <string>
#include <iostream>
using namespace std;

#if defined(_DEBUG)
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


void initContact ( stringstream & strm, LPCTSTR cname, long id, 
				   bool hasAttributes = false,
				   bool showToDo = false )
{
	if ( hasAttributes )
		strm << "<Contact language=english hasEmail=false>" << endl;
	else
		strm << "<Contact>" << endl;
	strm << "<name>" << cname << "</name>" << endl;
	strm << "<id>" << id << "</id>" << endl;

	if ( showToDo )
	{
		strm << "<ToDo>" << endl;
		strm << "<Item></Item>" << endl;
		strm << "<Item/>" << endl;
		strm << "<Item>Call</Item>" << endl;
		strm << "<Item>Say Hello</Item>" << endl;
		strm << "<Item>Take Notes</Item>" << endl;
		strm << "<Item>HangUp</Item>" << endl;
		strm << "</ToDo>" << endl;

	}

	strm << "</Contact>" << endl;
}

int main(int argc, char* argv[])
{
	// setup mem check
	#if defined(_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif


	// notice that no xml document declaration is included nor
	// is a schema included. if those exist in your buffer dont
	// send them to the parser. later will add the code to 
	// step through these. so this is a nonvalidating parser.
	// there is one bug in the parser. when an empty node
	// is encountered it will be reported as an element
	// this will be fixed later. if you have any suggestions
	// or improvements let me know.

	// set buffer
	long id = 100;

	stringstream strm;
	strm << "<Contacts>" << endl;

	strm << "<Contact/>" << endl;

	initContact( strm, "Joe Blow", 100 );
	initContact( strm, "John Doe", 500 );
	initContact( strm, "Mary Jane", 400, true, true );
	initContact( strm, "Wanda Ward", 400 );

	strm << "</Contacts>" << endl;


	string str = strm.str();

	// parse document
	XmlStream xml;
	xml.parse( (char *) str.c_str(), str.size() - 1 );

	while ( !kbhit() );

	return 0;
}




By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions