Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / XML

Read and Write application parameters in XML

Rate me:
Please Sign up or sign in to vote.
4.38/5 (33 votes)
30 Jun 20034 min read 1.6M   6.8K   141  
This article provides an easy way to load and save the parameters of an application in XML format.
#include "XmlStream.h"
#include "XmlParser.h"

using std::string;

// parser id for debugging
long XmlParser::_parseid = 0;

// notify methods
void XmlStream::foundNode ( string & name, string & attributes )
{
	// if no name stop
	if ( name.empty() )
		return;
  
	// tell subscriber
	if ( _subscriber )
		_subscriber->foundNode(name,attributes);
}

// ARNAUD
void XmlStream::endNode( string & name, string & attributes )
{
	if ( _subscriber )
		_subscriber->endNode(name, attributes);
}

void XmlStream::foundElement ( string & name, string & value, string & attributes )
{
	// if no name stop
	if ( name.empty() )
		return;

	// tell subscriber
	if ( _subscriber )
		_subscriber->foundElement(name,value,attributes);
}

bool XmlStream::parseNodes ( XmlParser & parser, const char * buffer, long parseLength )
{
	// #DGH note
	// need to address a null node within another node
	// i.e.
	// <Contacts>
	//		<Contact/>
	// </Contacts>
	// in this instance Contact will be reported as an
	// element 

	// debug info

	// while parse success, note for the first parser
	// this set the internal state also
	while ( parser.parse(buffer,parseLength) )
	{
		// if the value has a tag marker
      if(!parser.isSpecialXMLTag())
      {
		   if ( parser.valueHasTag() )
		   {
			   // show found node
			   string   name		 = parser.getName();
			   string	 attributes  = parser.getAttributes();

			   foundNode( name, attributes );

			   // get the parse state
			   long valueLength;
			   char * valueBuffer = parser.getValueState(valueLength);

			   // if parsing the node fails
			   XmlParser parserNode;
			   if ( !parseNodes(parserNode,valueBuffer,valueLength) )
				   return false;

			   // ARNAUD
			   endNode(name, attributes);

			   // if new parse cur position is in the last parser
			   // last tag position we are done with the node
			   char * curPos     = parserNode.getCurPos();
			   char * lastCurPos = parser.getLastTagPos();
			   if ( curPos >= lastCurPos )
			   {
				   break;
			   }
		   }
		   else
		   {
			   // show found element
			   string   name		 = parser.getName();
			   string   value		 = parser.getValue();
			   string	 attributes  = parser.getAttributes();

			   foundElement(name,value,attributes);
		   }
      }

		// get new parse state
		buffer = parser.getParseState(parseLength);
	}

	return true;
}



// parse the buffer

bool XmlStream::parse ( const char * buffer, long length )
{
	// if invalid stop
	if ( !buffer || length <= 0 )
		return false;

	// debug info
	assign( buffer, length );

	// declare parser
	XmlParser parser;

	// parse nodes
	bool docParsed = parseNodes(parser,buffer,length);

	return docParsed;
}

XmlStream ::XmlStream () :
//		std::string(),
	_subscriber(NULL)
{}

XmlStream ::~XmlStream ()
{ 
	release(); 
}

// release resources
bool XmlStream ::create ()
{
	return true;
}

bool XmlStream ::create ( char * buffer, long len )
{
	if ( buffer && len > 0 )
	{
		assign( buffer, len );
		return true;
	}
	else
		return false;
}

void XmlStream ::release ()
{
	erase( begin(), end() );
}

// get/set subscriber
bool XmlStream ::hasSubscriber ()
{
	if ( _subscriber )
		return true;
	else
		return false;
}

XmlNotify * XmlStream ::getSubscriber ()
{
	return _subscriber;
}

void XmlStream ::setSubscriber ( XmlNotify & set )
{
	_subscriber = &set;
}


// get ref to tag stream
XmlStream & XmlStream ::getTagStream ()
{ return *this; }


// get string ref
std::string & XmlStream ::str()
{ return (std::string &) *this; }



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
Web Developer
China China
I've been living & working in Tokyo since 2000 . I'm currently working in Gentech Corp. (www.gen.co.jp), developing computer vision applications, especially in the field of face detection.

I've been interested in C++ for quite a while and recently discovered Ruby.

My hobbies are trekking, japanese food, yoga & onsens.

Comments and Discussions