Click here to Skip to main content
15,892,072 members
Articles / Programming Languages / C++

A STL based XML Config Tool

Rate me:
Please Sign up or sign in to vote.
3.19/5 (9 votes)
19 Jun 20051 min read 69.9K   886   38  
Writing multiple enumerated configuration entries.
// XmlReadWrite.cpp : Definiert den Einsprungpunkt f�r die Anwendung.
#include "stdafx.h" 
#undef _PARAMIO_CSTRING_SUPPORT_
#include <string>
#include <conio.h>
#include "C:\tmp\Tool Classes\ParamIO\XmlConfig\XmlConfig.h"
using namespace std;

//those too functions yust demonstrate a bit of the functionality of the ParamIO written by Arnaud Bruion
void ExtractElements( XML_Node& aNode )
{
	XML_Node::elements_const_iterator iterBegin = aNode.beginElements();
	XML_Node::elements_const_iterator iterEnd = aNode.endElements();
	for(XML_Node::elements_const_iterator iter = iterBegin; iter != iterEnd; iter++ )
	{
		std::pair<std::string, Element > aPair = *iter;
		std::string strName = aPair.first;
		cout << "Element Name : "<< strName << '\n';
		Element aEl = aPair.second;
		cout << "Element Attributes : "<< aEl.attributes << '\n';
		int dummy = 0;

	}
}

void ExtractNode( XML_Node& aNode )
{
	XML_Node::nodes_const_iterator iterBegin = aNode.beginNodes();
	XML_Node::nodes_const_iterator iterEnd = aNode.endNodes();
	for( XML_Node::nodes_const_iterator iter = iterBegin;iter != iterEnd; iter++ )
	{
		XML_Node node = (*iter);
		string strNodeName = node.getName();
		cout << "Found a Node : "<< strNodeName << '\n';
		ExtractElements( node );
		ExtractNode( node );
		cout << '\n';
	}
}

//I let this sample code in, so that everyone would be able to have a look at the way it is possible to work with
//XMLConfig and ParamIO
int main(int argc, char* argv[])
{
 	/*ParamIO aParam;
	aParam.readFile( "c:\\test.xml" );
	string strVal;
	string strDefault = "NOT_FOUND";
	aParam.read( "CCSDicomClientConfig:WorklistTags:Tag", strVal, strDefault );
	XML_Param_Notify TheTree = aParam.getTree();
	ParamIO TheWlTree;
	aParam.extract( "CCSDicomClientConfig:WorklistTags", TheWlTree );
	TheTree = TheWlTree.getTree();
	XML_Node::nodes_iterator NodesIter = TheTree.top();
	XML_Node aNode = *NodesIter;
	ExtractElements( aNode );
	ExtractNode( aNode );*/
	/*XmlConfig aCnfg( "c:\\test.xml" );//read in a Sample - file
	aCnfg.ReadSubtree( "CCSDicomClientConfig:WorklistTags" );
	int iGroup[24];
	int iElement[24];
	std::string strValue[24];
	int iReadVals = aCnfg.GetReadValueCount();
	for( int k = 0; k < iReadVals; k++ )
	{
		//aCnfg.GetValue( k, 0/*string("Group")*///, iGroup[k], -1 );
		//aCnfg.GetValue( k, 1/*string("Element")*/, iElement[k], -1 );
		//aCnfg.GetValue( k, 2/*string("Val")*/, strValue[k], string("No") );
/*		cout << "Found a DICOM Tag Group : ";
		cout << iGroup[k];
		cout << " Element : ";
		cout << iElement[k];
		cout << " Setting : ";
		cout << strValue[k] << '\n';
	}
	string strSubNode;
	int iNumSubNodes = aCnfg.hasSubNodes( "CCSDicomClientConfig:WorklistTags", strSubNode );
	if( iNumSubNodes )
	{
		bool bSuccess = aCnfg.enterSubTree( strSubNode );
		iReadVals = aCnfg.ReadCurrentTree();
		cout << "Found a SubKey Named : " << strSubNode << '\n';
		for( int k = 0; k < iReadVals; k++ )
		{
			aCnfg.GetValue( k, string("Group"), iGroup[k], -1 );
			aCnfg.GetValue( k, string("Element"), iElement[k], -1 );
			aCnfg.GetValue( k, string("Val"), strValue[k], string("No") );
			cout << "Found a DICOM Tag Group : ";
			cout << iGroup[k];
			cout << " Element : ";
			cout << iElement[k];
			cout << " Setting : ";
			cout << strValue[k] << '\n';
		}
	}
	//aCnfg.write( "CCSDicomClientConfig:WorklistTags:Test", 100 );
	//bool bSuccessErase = aCnfg.erase( "CCSDicomClientConfig:Communication:ImageServer:LokalerPort" );
	int iAddedAttributes = aCnfg.AddAttributes( "CCSDicomClientConfig:WorklistTags:Tag", VAIUE_INT,"aTestVal", 10, "0010", "0010" );
	int iRemovedAttributes =  aCnfg.RemoveAttributes( "CCSDicomClientConfig:WorklistTags:Tag", "intGroup", "0010" );
	
	aCnfg.writeFile( "c:\\out.xml" );//to control the modifications*/
	XmlConfig aParam;
	aParam.write( "Test:Blub:SecondNode", "Whatever" );
	aParam.AddAttributeString( "Test:FirstNode:Blub:SomeAttrib", "stringAttrib = \"SomeValue\" intVal = \"12345\"", 0 );
	aParam.AddAttributeString( "Test:FirstNode:Blub:SomeAttrib", "stringAttrib = \"SomeOtherValue\" intVal = \"12345678\"", 0 );
	aParam.writeFile( "c:\\Blub.xml" );
	aParam.readFile( "c:\\Blub.xml" );
	string strVal;
	aParam.read( "Blub:SecondNode", strVal, string() );
	aParam.ReadSubtree( "FirstNode:Blub" );
	int iReadVals = aParam.GetReadValueCount();
	for( int k = 0; k < iReadVals; k++ )
	{
		aParam.GetValue( k, string("Attrib"), strVal, string("NONE") );
		cout << strVal << endl;
	}
	cout << "Finished";
	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
Software Developer
Germany Germany
Studying and having a degree of medical engineering, but decided to work as a software developer writing medical applications for image processing.

Comments and Discussions