Click here to Skip to main content
14,334,974 members

Read and Write application parameters in XML

Rate this:
4.38 (33 votes)
Please Sign up or sign in to vote.
4.38 (33 votes)
30 Jun 2003
This article provides an easy way to load and save the parameters of an application in XML format.

Sample Image - maximum width is 600 pixels


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:

  <TEXT>Hello world</TEXT>
Let's say we have the following variables in the application :
std::string _text;                 // The text
int _red, _green, _blue;           // color
std::string _fontName;             // font name
double _fontSize;                  // font size
The code to write the previous file will be :
ParamIO outXml;

outXml.write("PARAMS:TEXT", _text);

outXml.write("PARAMS:COLOR:RED",   _red);
outXml.write("PARAMS:COLOR:GREEN", _green);
outXml.write("PARAMS:COLOR:BLUE",  _blue);

outXml.write("PARAMS:FONT:NAME", _fontName);
outXml.write("PARAMS:FONT:SIZE", _fontSize);

outXml.writeFile("filename.xml"); // Finally write the file to disk
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 :
ParamIO inXml;

inXml.readFile("filename.xml"); // Read the file from disk"PARAMS:TEXT", _text, std::string("Hello world"));"PARAMS:COLOR:RED",   _red,   0);"PARAMS:COLOR:GREEN", _green, 0);"PARAMS:COLOR:BLUE",  _blue,  0);"PARAMS:FONT:NAME", _fontName, std::string("Arial"));"PARAMS:FONT:SIZE", _fontSize, 12.0);
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.


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.


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


About the Author

Arnaud Brejeon
Web Developer
China China
I've been living & working in Tokyo since 2000 . I'm currently working in Gentech Corp. (, 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

GeneralRe: Bug fix Pin
31-May-01 4:12
suss31-May-01 4:12 
GeneralRe: Bug fix Pin
Mark Marsh9-Jun-01 7:16
memberMark Marsh9-Jun-01 7:16 
GeneralRe: Bug fix Pin
Arnaud Brejeon10-Jun-01 15:15
memberArnaud Brejeon10-Jun-01 15:15 
GeneralXML Pin
pathi29-May-01 18:47
memberpathi29-May-01 18:47 
GeneralRe: XML Pin
Arnaud Brejeon29-May-01 18:56
memberArnaud Brejeon29-May-01 18:56 
GeneralA bug.. Pin
Jim A. Johnson29-May-01 8:08
memberJim A. Johnson29-May-01 8:08 
GeneralRe: A bug.. Pin
Jim A. Johnson29-May-01 8:14
memberJim A. Johnson29-May-01 8:14 
GeneralRe: A bug.. Pin
Ajit Jadhav29-May-01 9:03
memberAjit Jadhav29-May-01 9:03 
I haven't tried out the software in the article, but I can say this much: It has to be a feature not a bug! Cry | :(( but true. XML documents are required to have exactly one root node, by W3C XML Spec.

- TheOneAndOnlyRootNode
    - PARAM
    - YADDA1
    - YADDA2
    - etc
- You can have comments and other miscellaneous stuff apart from a data root node, however. Pl. consult the spec for multiplicity; I am not sure off-hand whether Misc or Misc* is applicable.

Nature, to be commanded, must be obeyed. (Francis Bacon)
Nature, to be apprehended, must be obeyed. (Ayn Rand)

GeneralRe: A bug.. Pin
Jim A. Johnson29-May-01 9:09
memberJim A. Johnson29-May-01 9:09 
GeneralRe: A bug.. Pin
Arnaud Brejeon29-May-01 15:23
memberArnaud Brejeon29-May-01 15:23 
GeneralRe: A bug.. Pin
Ruination25-Aug-03 7:57
memberRuination25-Aug-03 7:57 
GeneralRe: A bug.. Pin
Ruination25-Aug-03 10:08
memberRuination25-Aug-03 10:08 
QuestionWhat's about UNICODE? Pin
Holger Persch28-May-01 21:14
memberHolger Persch28-May-01 21:14 
AnswerRe: What's about UNICODE? Pin
Arnaud Brejeon29-May-01 15:20
memberArnaud Brejeon29-May-01 15:20 
QuestionAmateur code? Pin
28-May-01 5:03
suss28-May-01 5:03 
AnswerRe: Amateur code? Pin
29-May-01 12:03
suss29-May-01 12:03 
AnswerRe: Amateur code? Pin
Arnaud Brejeon29-May-01 15:15
memberArnaud Brejeon29-May-01 15:15 
GeneralRe: Amateur code? Pin
19-Aug-01 21:59
suss19-Aug-01 21:59 
GeneralRe: Amateur code? Pin
[James Pullicino]20-Aug-01 0:22
member[James Pullicino]20-Aug-01 0:22 
GeneralRe: Amateur code? Pin
Thomas Freudenberg26-Nov-01 3:46
memberThomas Freudenberg26-Nov-01 3:46 
GeneralRe: Amateur code? Pin
Arnaud Brejeon26-Nov-01 14:19
memberArnaud Brejeon26-Nov-01 14:19 
GeneralRe: Amateur code? Pin
Tim Smith10-Feb-02 3:12
memberTim Smith10-Feb-02 3:12 
AnswerRe: Amateur code? Pin
7-Sep-01 13:05
suss7-Sep-01 13:05 
AnswerRe: Amateur code? Pin
16-Nov-01 11:42
suss16-Nov-01 11:42 
AnswerRe: Amateur code? Pin
10-Feb-02 2:51
suss10-Feb-02 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 30 Apr 2001


140 bookmarked