65.9K
CodeProject is changing. Read more.
Home

A STL based XML Config Tool

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.19/5 (9 votes)

Jul 28, 2004

1 min read

viewsIcon

72416

downloadIcon

887

Writing multiple enumerated configuration entries.

Introduction

In an application, I needed the possibility to have an easy way for reading and writing configuration data to a file. Further, I needed the possibility to nest different Settings into each other for a better representation of how the data is in relation to each other. XML seemed to be the solution. Not having any knowledge about XML, I found two great articles on CodeProject: Read and Write application parameters in XML from Arnaud Brejon, and A simple STL based XML parser by David Hubbard.

The problem using this code was that it was not able to use equal names of configuration keys and that would lead to the need of enumeration. I did an enumeration of different keys before using INI – files. That is really ugly if you need to delete keys, because you have to change the whole enumeration of keys after the deleted one. The solution would be a configuration tool allowing keys of equal names. Many developers seemed to have the same needs, and so I hope that this article helps a bit.

All I had to do was to add some little code and write a wrapper class around Arnoud Brejon's ParamIO - class. The data written, when using equal entry names, is embedded into the XML Nodes attributes.

Using the code

All you need is the XmlConfig – class that actually wraps a ParamIO object.

    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" );

A kind of type checking is already included (but not handled yet). The parser interprets the "int" in intVal to determine which type the value is. If no matching (or supported) type is found, a normal string is assumed.