Click here to Skip to main content
15,880,725 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.
#if !defined(XmlParser_H)
#define XmlParser_H

#include "XmlUtil.h"

#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>


//////////////////////////////////////////////////////////////////////////////
// XmlParserr
//
// Purpose: used to parse a buffer that has xml formatted data

class XmlParser
{
public:

	static long _parseid;
	
	// id of parser (for debugging)
	long		_id;

	// provides string interface to buffer
	std::string		_strParse;

	// parse buffer and offsets of where we are parsing
	char *		_buffer;
	long		_parseLength;

	// current position in parse buffer
	long		_current;

	// tag, attributes and data position information
	long		_firstTagStart;
	long		_firstTagEnd;

	long		_lastTagStart;
	long		_lastTagEnd;

	long		_nameStart;
	long		_nameEnd;
	long		_attrStart;
	long		_attrEnd;
	long		_valueStart;
	long		_valueEnd;


	XmlParser ();

	// create/release
	bool create ( const char * buffer, long parseStart, long parseEnd );

	bool create ( const char * buffer, long length );

	void release ();

	// cur position methods
	long getCurrent ();

	long getParseLength ();

	long getCurLength ();

	long getOffsetLength ( long offset );


	char * getBufferPos ();

	char * getLastBufferPos ();

	char * getCurPos ();

	char * getParseState ( long & parseLength );

	// get ref to parse buffer
	std::string & str ();

	// state methods
	void reset ();

	bool isValid ();

	void resetTagPositions ( long start=-1 );
	
	void clear ();

	// parse methods
	bool parse ( const char * buffer, long parseStart, long parseEnd );

	bool parse ( const char * buffer, long parseLength );

	bool parse ();
	bool parse ( std::string & name,
				 std::string & value,
				 std::string & attributes,
				 long & current );



	// tag search methods

	bool hasNullTag ();
   bool isSpecialXMLTag();

	/////////////////////////////////////////////////////////
	// these methods are protected because the state
	// of parsing might not be properly setup, and
	// if that were so then calling these methods
	// would cause errors

protected:

	// name search methods
	bool parseName ();
	bool parseName ( std::string & name );

	// attribute search methods
	bool parseAttributes ();
	bool parseAttributes ( std::string & attributes );

	// data search methods
	bool parseValue ();

	bool parseValue ( std::string & value );

public:

	// name access methods
	char * getNamePos ();

	bool hasName ();

	long getNameLength ();


	std::string getName ();

	// attribute access methods
	char * getAttributesPos ();

	bool hasAttributes ();

	long getAttributesLength ();

	std::string getAttributes ();

	// value access methods
	char * getValuePos ();

	bool hasValue ();

	long getValueLength ();

	std::string getValue ();

	char * getValueState ( long & valueLength );

	bool valueHasTag ();

	// tag access methods
	long getTagLength ();

	long getLastTagLength ();

	bool hasTag ();

	bool hasLastTag ();

	char * getTagPos ();

	char * getLastTagPos ();

	std::string getTag ();

	// string utility methods
	long getLength ( long startPos,
			         long endPos );

	std::string::iterator begin ();

	std::string ::iterator end ();

	long find ( char srchChar, long offset, long length = -1 );

	long find ( char * srchStr, long offset, long length = -1 );

	long find ( std::string & srchStr, long offset, long length = -1 );

	long rfind ( char srchChar, long offset, long length );

	long rfind ( char * srchStr, long offset, long length );

	std::string substr ( long offset, long length );
};


#endif

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