Click here to Skip to main content
11,412,833 members (74,695 online)
Click here to Skip to main content

Read and Write application parameters in XML

, 30 Jun 2003
Rate this:
Please Sign up or sign in to vote.
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

<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->


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

QuestionErrors in VS2005 Pin
Member 8328498 at 6-Jan-12 0:33
memberMember 83284986-Jan-12 0:33 
QuestionVS2010 compilation error Pin
TheSpad at 1-Sep-11 0:50
memberTheSpad1-Sep-11 0:50 
AnswerRe: VS2010 compilation error Pin
Sebastian Michalski at 7-Aug-12 0:43
memberSebastian Michalski7-Aug-12 0:43 
Generalgood code Pin
Donsw at 8-May-09 17:17
memberDonsw8-May-09 17:17 
GeneralUpdate to XML ParamIO for Visual Studio 2008 Pin
Rudolf Cardinal at 24-Apr-09 4:48
memberRudolf Cardinal24-Apr-09 4:48 
GeneralAttribute Read/Write Needed.... Pin
JasonAw at 23-Mar-09 17:22
memberJasonAw23-Mar-09 17:22 
Generalsome clarification Pin
vinodkumar subramaney at 31-Jan-09 6:59
membervinodkumar subramaney31-Jan-09 6:59 
Generalthank u da mandaya Pin
prithivintpl at 22-May-08 2:41
memberprithivintpl22-May-08 2:41 
Generalthank you for your code Pin
tjroamer at 12-May-08 7:08
membertjroamer12-May-08 7:08 
Question as first line in the file Pin
Armandopoulos at 26-Apr-08 11:04
memberArmandopoulos26-Apr-08 11:04 
Questionapplication parameter Pin
nithin kumar at 4-Dec-07 20:11
membernithin kumar4-Dec-07 20:11 
QuestionHi anyone got this working on VS2005? Pin
magiceyes118 at 23-Jul-07 18:46
membermagiceyes11823-Jul-07 18:46 
GeneralAttributes again Pin
samcct at 31-Oct-06 7:24
membersamcct31-Oct-06 7:24 
QuestionParamIO and VisualStudio2005 Pin
mexicanchili at 19-Sep-06 3:11
membermexicanchili19-Sep-06 3:11 
AnswerRe: ParamIO and VisualStudio2005 Pin
gyarmit at 8-Jan-07 5:07
membergyarmit8-Jan-07 5:07 
GeneralNode searching error Pin
Gintas at 28-Oct-05 9:36
memberGintas28-Oct-05 9:36 
Generalstatic_cast error Pin
Apex Watson at 22-Aug-05 11:25
memberApex Watson22-Aug-05 11:25 
GeneralRe: static_cast error Pin
Anonymous at 31-Aug-05 13:46
sussAnonymous31-Aug-05 13:46 
GeneralW3C compliance Pin
Umut Alev at 25-Jul-05 22:03
memberUmut Alev25-Jul-05 22:03 
GeneralReading with two or more same elements Pin
sdarisi at 27-Apr-05 0:12
membersdarisi27-Apr-05 0:12 
GeneralFor Dev-C++ 3.4.2 Pin
V.Kotov at 17-Mar-05 0:55
memberV.Kotov17-Mar-05 0:55 
GeneralRe: For Dev-C++ 3.4.2 Pin
Denis Chekhlov at 16-May-05 7:54
memberDenis Chekhlov16-May-05 7:54 
GeneralWhite Spaces Pin
bdm1 at 4-Mar-05 8:11
sussbdm14-Mar-05 8:11 
GeneralUpdate Pin
bobbymihalca at 8-Feb-05 23:32
memberbobbymihalca8-Feb-05 23:32 
GeneralRe: Update Pin
Arnaud Brejeon at 10-Feb-05 8:46
memberArnaud Brejeon10-Feb-05 8:46 
Generallinker errors on new Project Pin
Lennin at 7-Jun-04 5:36
memberLennin7-Jun-04 5:36 
QuestionwriteFile() will truncate the existing file ??? Pin
songge at 18-May-04 6:35
membersongge18-May-04 6:35 
Questionkey and value in the xml file ? Pin
Dominque le Gros at 23-Apr-04 0:08
memberDominque le Gros23-Apr-04 0:08 
GeneralThanks Pin
TheBigW at 1-Apr-04 0:38
memberTheBigW1-Apr-04 0:38 
GeneralRe: Thanks Pin
Arnaud Brejeon at 3-Apr-04 1:55
memberArnaud Brejeon3-Apr-04 1:55 
GeneralRe: Thanks Pin
Nelis Willers at 6-Jun-04 13:13
memberNelis Willers6-Jun-04 13:13 
GeneralRe: Thanks Pin
TheBigW at 6-Jun-04 22:52
memberTheBigW6-Jun-04 22:52 
GeneralRe: Thanks Pin
graysong at 14-Jun-04 8:07
membergraysong14-Jun-04 8:07 
GeneralRe: Thanks Pin
boz at 27-Jul-04 16:13
memberboz27-Jul-04 16:13 
GeneralRe: Thanks Pin
TheBigW at 28-Jul-04 4:17
memberTheBigW28-Jul-04 4:17 
QuestionUsing the ParamIO ??? Pin
Dominque le Gros at 17-Mar-04 6:55
memberDominque le Gros17-Mar-04 6:55 
GeneralUNICODE -- Very Important Pin
ExtraLean at 20-Feb-04 9:30
memberExtraLean20-Feb-04 9:30 
GeneralRe: UNICODE -- Very Important Pin
ExtraLean at 20-Feb-04 10:03
memberExtraLean20-Feb-04 10:03 
GeneralRe: UNICODE -- Very Important Pin
Arnaud Brejeon at 20-Feb-04 22:53
memberArnaud Brejeon20-Feb-04 22:53 
QuestionHow to change the attributes of xml Pin
Boggarapu Sateesh Kumar at 14-Feb-04 3:01
memberBoggarapu Sateesh Kumar14-Feb-04 3:01 
GeneralIm having problems Pin
Tauri at 29-Jan-04 7:37
memberTauri29-Jan-04 7:37 
GeneralRe: Im having problems Pin
Nitron at 19-Feb-04 10:47
memberNitron19-Feb-04 10:47 
GeneralPerfect! Pin
Nitron at 7-Jan-04 8:13
memberNitron7-Jan-04 8:13 
GeneralCannot compile in VC++6 - returns 252 errors! Pin
Andrew Fox at 24-Dec-03 3:50
memberAndrew Fox24-Dec-03 3:50 
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Arnaud Brejeon at 24-Dec-03 5:43
memberArnaud Brejeon24-Dec-03 5:43 

can you explain the problem by giving the output of the compiler?
I'm sure it's a small difference that makes all the errors.
First of all, can you compile properly the provided sample application?
I'll try to do my best to help you, but I don't have VC++ at the moment so I can't try many things.

GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Andrew Fox at 24-Dec-03 6:15
memberAndrew Fox24-Dec-03 6:15 
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Arnaud Brejeon at 27-Dec-03 23:17
memberArnaud Brejeon27-Dec-03 23:17 
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Andrew Fox at 27-Dec-03 23:53
memberAndrew Fox27-Dec-03 23:53 
GeneralThis won't compile with Visual Studio .NET 2003 Pin
benjamino at 19-Aug-03 5:40
memberbenjamino19-Aug-03 5:40 
GeneralRe: This won't compile with Visual Studio .NET 2003 Pin
bobbymihalca at 19-Dec-03 6:43
memberbobbymihalca19-Dec-03 6:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 1 Jul 2003
Article Copyright 2001 by Arnaud Brejeon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid