,

Introduction

This article provides an easy way to load and save the parameters of an application in XML format.

XML is a convenient format to deal with the parameters of applications for the following reasons:

It's text, so you can easily check the values and modify them with a classical editor (by the way, IE 5.0 shows XML in a very nice way).

It's based on a tree so you can have your parameters saved in a structured way.

It's platform and language independent.

My goal was not to parse an XML file, but to give an easy way to read and write the data. So the code is based on A simple STL based XML parser by David Hubbard. I would really like to thank him for his parser, it's a very good work! Moreover it saved me a lot of time, even if I made a few modifications in his original code to fulfill my goals.

The code is entirely based on STL. It compiles on linux, too. I guess it will compile on any other platform/compiler that supports STL.

Using the code

Now let's take an example to explain more precisely what you can do with ParamIO. Let's assume you have an application to show some text on the screen in a given color and in a given font (the demo project). The parameters of your application will be the text (1 string), the color (3 values RGB) and the font (1 string for the font name and 1 double for the font size). You want to be able to save those parameters on the disk and load them for a later use. A nice XML file containing those values can look like this:

As you can see, it's very easy. In the write method, the first parameter is a char* that defines the position in the XML tree. This is similar to XPath, but I didn't know XPath at the time I wrote this code, I would have used the '/' instead if I had known. The second parameter gives the value you want to write. write is a templated method so you can write almost any kind of variable, from int to std::string. Reading the file is also straightforward :

You may be surprised by the third parameter of the read method. What does it mean? It's the default value of the parameter you try to read. Imagine you want to read a file like the previous one and the size of the font is not specified, then _fontSize will be automatically set to its default value 12.0. It's a very useful behaviour when you have several versions of a same application and some versions have parameters that didn't exist in older ones. It ensures that you still can read the old files, filling the missing values with default ones. It also lets you load a file that doesn't exist and set all the parameters with their default values.

ParamIO lets you read and write XML coming from streams also, it should be useful if you want some applications to exchange data through sockets for example.

The demo application includes a XML dialog box. With this dialog box (CXML_Dialog), you can visualise and modify any XML tree. I used it a lot, it's working really well. In case your parameter name contains FILENAME inside (e.g FONT_FILENAME), you'll see a button appear, and if you click on it, you'll obtain an open file dialog where you can find your file. I used this CXML_Dialog dialog box a lot and I'm very happy about it, it works really fine. CXML_Dialog uses a class from Easy Navigation Through an Editable List View by Lee Nowotny. I really thank him for the job, it works perfectly.

If you have any comments, suggestions or improvements let me know.

History

20 Nov 2002 : first version with GUI demo, with comparison capabilities

20 June 2003:

modified the website, added src download without the GUI demo

Added support for MFC CString has been added thanks to Paul Kissel : to enable CString read/write, just define _PARAMIO_CSTRING_SUPPORT_ in the compilation options.

Added a method to erase a node or an entire subtree as been added, thanks to Ogi's suggestion.

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.

I don't know if anybody still cares about ParamIO. I'm about to convert all our projects to VisualStudio2005.
ParamIO doesn't compile. I get tons of compiler errors like this:

xml_node.h(170) : error C2440: 'static_cast' : cannot convert from 'XML_Node *' to 'XML_Node::nodes_iterator'
No constructor could take the source type, or constructor overload resolution was ambiguous

Unluckily the programmer who wrote this particular project has left the company. I'm wondering if anybody probably has converted ParamIO successfully to VS2005 and could share his sources with me thus saving me a lot of work.

It appears that _node is a private variable of type XML_Node in the XML_Param_Notify class. XML_Node::nodes_const_iterator is just a typedef defined as:

typedef std::vector<XML_Node>::const_iterator nodes_const_iterator;

So it looks like this code is trying to cast the address of _node into an iterator of type nodes_const_iterator. An iterator is just a pointer to an address right? It seems like this should be doable, and I'm guessing that since no one else has posted this problem yet it has something to do with what I'm doing on my end. Any ideas?

Perhaps this has been talked a lot but I can not stop my self from writing this.
One of the reasons of XML is connecting platforms, apps through text encoded messages, to achieve this you need compliance. Implementing a fully (95%-98%) compliant XML parser is really hard. Usually an average Dev can get to 80% pretty quickly and the experienced can get to %90. The rest is all egde cases and getting them right requires extensive testing suites to ensure compat between different platforms. I know this by experience. There are quite a few xml parser implementations out there. Users are usually best served when the xml parser implementations are recognized by the W3C Recommendations. Other implementations can not go any further than academic studies as the cost of them making interop with the rest of the world sky rockets as dev's are trying to achieve %95 compat.

I am having trouble with reading in white spaces with this code. I can write strings that contain white spaces, but when I try to read them in, it only returns the first word. I'm assuming I'm not the only one out there with this problem, anyone have a fix?

Great codes! But in my usage, I have a problem,i.e. my program will call writeFile several times, so my data will be overwritten. For writeFile use the std::ofstream to write file, so existing file will be overwritten. I want to know: Is there any better way to avoid these?

David Hubbard wrote a great parser and you made it more usable. I need to write my own Config Writer, because I need some more functionality ( multiple entries of the same name, nesting of Setting into each other...), but the Functionality you added with the Handling for Nodes an Elements makes that quite easy.
Why did you add that CString support in such a nice "pure" STL Tool?

About CString, someone sent me an email with the changes to add the CString support because many people are interested in CString.
I want ParamIO to benefit as much as possible from other people, so I decided to add his code.
If you want to remove the CString support for your use, feel free to do it.