Click here to Skip to main content
11,429,154 members (62,329 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) -->

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:

<PARAMS>
  <TEXT>Hello world</TEXT>
  <COLOR>
	<RED>50</RED>
	<GREEN>128</GREEN>
	<BLUE>255</BLUE>
  </COLOR>
  <FONT>
	<NAME>Arial</NAME>
	<SIZE>12.0</SIZE>
  </FONT>
</PARAMS>
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

inXml.read("PARAMS:TEXT", _text, std::string("Hello world"));

inXml.read("PARAMS:COLOR:RED",   _red,   0);
inXml.read("PARAMS:COLOR:GREEN", _green, 0);
inXml.read("PARAMS:COLOR:BLUE",  _blue,  0);

inXml.read("PARAMS:FONT:NAME", _fontName, std::string("Arial"));
inXml.read("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.

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.

A list of licenses authors might use can be found here

Share

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. (www.gen.co.jp), 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: Enhancement Idea Pin
Arnaud Brejeon25-Nov-02 18:32
memberArnaud Brejeon25-Nov-02 18:32 
GeneralRe: Enhancement Idea Pin
Loren Brewer26-Nov-02 8:37
memberLoren Brewer26-Nov-02 8:37 
GeneralRe: Enhancement Idea Pin
Neville Franks26-Nov-02 15:28
memberNeville Franks26-Nov-02 15:28 
GeneralComments Pin
Loren Brewer21-Nov-02 11:24
memberLoren Brewer21-Nov-02 11:24 
GeneralXML Editor Pin
doron21-Nov-02 3:56
memberdoron21-Nov-02 3:56 
GeneralRe: XML Editor Pin
Nitron10-Jan-03 14:36
memberNitron10-Jan-03 14:36 
GeneralTabs Pin
Loren Brewer14-Nov-02 20:56
memberLoren Brewer14-Nov-02 20:56 
GeneralRe: Tabs Pin
Neville Franks25-Nov-02 11:19
memberNeville Franks25-Nov-02 11:19 
GeneralRe: Tabs Pin
Arnaud Brejeon25-Nov-02 18:36
memberArnaud Brejeon25-Nov-02 18:36 
GeneralRe: Tabs Pin
Loren Brewer26-Nov-02 8:33
memberLoren Brewer26-Nov-02 8:33 
GeneralRe: Tabs Pin
Neville Franks26-Nov-02 15:25
memberNeville Franks26-Nov-02 15:25 
GeneralVS.NET compile errors Pin
Anonymous27-Oct-02 16:21
sussAnonymous27-Oct-02 16:21 
GeneralCannot compile in VS.NET Pin
Damon Chitsaz7-Sep-02 10:20
memberDamon Chitsaz7-Sep-02 10:20 
GeneralVC++ 5 Pin
Ikke8-Jul-02 3:13
memberIkke8-Jul-02 3:13 
Has someone got this code to work using VC++ 5? Maybe this is a stupid question, but
1. I can't compile the demo project and
2. I created another project where I have to use these functions, I included all the necessary cpp and h files in the project and included ParamIO.h in my main file. But I can't compile. The compiler always tells me that the class XML_Node has no constructor, and when I double click the exception, I'm sent to a file Vector.h

Any help? Could it be possible to pre-build a .lib file with the (compiled) functions in it, o I still could use this class in my program?

Thanks, Ikke
GeneralRe: VC++ 5 Pin
Arnaud Brejeon8-Jul-02 16:10
memberArnaud Brejeon8-Jul-02 16:10 
GeneralRe: VC++ 5 Pin
Ikke12-Jul-02 2:21
memberIkke12-Jul-02 2:21 
GeneralXml Tree Pin
anonimous25-Jun-02 11:19
memberanonimous25-Jun-02 11:19 
Generaladd attributes Pin
Redy9-Jun-02 6:27
memberRedy9-Jun-02 6:27 
GeneralRe: add attributes Pin
critta13-Dec-02 4:40
membercritta13-Dec-02 4:40 
GeneralC# Version Required Pin
Anonymous7-Apr-02 7:07
memberAnonymous7-Apr-02 7:07 
GeneralRe: C# Version Required Pin
Anonymous22-May-02 1:23
memberAnonymous22-May-02 1:23 
QuestionHow can I use this parser without afx.....h Pin
TomasN15-Mar-02 3:11
memberTomasN15-Mar-02 3:11 
AnswerRe: How can I use this parser without afx.....h Pin
markkuk15-Mar-02 3:40
membermarkkuk15-Mar-02 3:40 
GeneralAlphabetical order. Pin
Jonny Newman14-Feb-02 9:37
memberJonny Newman14-Feb-02 9:37 
GeneralRe: Alphabetical order. Pin
Arnaud Brejeon18-Feb-02 0:49
memberArnaud Brejeon18-Feb-02 0:49 
GeneralRe: Alphabetical order. Pin
Arnaud Brejeon18-Feb-02 1:06
memberArnaud Brejeon18-Feb-02 1:06 
GeneralRe: Alphabetical order. Pin
Jonny Newman1-Mar-02 7:45
memberJonny Newman1-Mar-02 7:45 
GeneralRe: Alphabetical order. Pin
Atlantys18-Apr-02 13:10
memberAtlantys18-Apr-02 13:10 
GeneralRe: Alphabetical order. Pin
Loren Brewer14-Nov-02 21:06
memberLoren Brewer14-Nov-02 21:06 
GeneralRe: Alphabetical order. Pin
Arnaud Brejeon17-Nov-02 15:01
memberArnaud Brejeon17-Nov-02 15:01 
GeneralRe: Alphabetical order. Pin
Arnaud Brejeon18-Nov-02 15:15
memberArnaud Brejeon18-Nov-02 15:15 
QuestionHow can I rewrite only one value instead of rewrite all? Pin
zengjun8-Sep-01 6:22
memberzengjun8-Sep-01 6:22 
AnswerRe: How can I rewrite only one value instead of rewrite all? Pin
Arnaud Brejeon8-Sep-01 19:27
memberArnaud Brejeon8-Sep-01 19:27 
GeneralRe: How can I rewrite only one value instead of rewrite all? Pin
Anonymous9-Sep-01 6:27
memberAnonymous9-Sep-01 6:27 
GeneralRe: How can I rewrite only one value instead of rewrite all? Pin
Joerg Hoffmann26-Oct-01 1:10
memberJoerg Hoffmann26-Oct-01 1:10 
GeneralRe: How can I rewrite only one value instead of rewrite all? Pin
Arnaud Brejeon28-Oct-01 14:53
memberArnaud Brejeon28-Oct-01 14:53 
GeneralI did not get solution.Please help! Pin
R.selvam13-Oct-03 4:26
memberR.selvam13-Oct-03 4:26 
Generalread/write XML-file from HTML(script) Pin
Maxua5-Jul-01 14:39
memberMaxua5-Jul-01 14:39 
QuestionHow to use XML lists ? Pin
Anonymous18-Jun-01 23:05
memberAnonymous18-Jun-01 23:05 
AnswerRe: How to use XML lists ? Pin
Anonymous18-Jun-01 23:07
memberAnonymous18-Jun-01 23:07 
GeneralRe: How to use XML lists ? Pin
Arnaud Brejeon18-Jun-01 23:18
memberArnaud Brejeon18-Jun-01 23:18 
GeneralParameter problems Pin
W. Scott Dillman30-May-01 10:09
memberW. Scott Dillman30-May-01 10:09 
GeneralRe: Parameter problems Pin
Arnaud Brejeon30-May-01 16:30
memberArnaud Brejeon30-May-01 16:30 
GeneralBug fix Pin
Arnaud Brejeon30-May-01 20:12
memberArnaud Brejeon30-May-01 20:12 
GeneralRe: Bug fix Pin
W. Scott Dillman31-May-01 5:12
memberW. Scott Dillman31-May-01 5:12 
GeneralRe: Bug fix Pin
Mark Marsh9-Jun-01 8:16
memberMark Marsh9-Jun-01 8:16 
GeneralRe: Bug fix Pin
Arnaud Brejeon10-Jun-01 16:15
memberArnaud Brejeon10-Jun-01 16:15 
GeneralXML Pin
pathi29-May-01 19:47
memberpathi29-May-01 19:47 
GeneralRe: XML Pin
Arnaud Brejeon29-May-01 19:56
memberArnaud Brejeon29-May-01 19:56 
GeneralA bug.. Pin
Jim A. Johnson29-May-01 9:08
memberJim A. Johnson29-May-01 9:08 

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
Web02 | 2.8.150428.2 | Last Updated 1 Jul 2003
Article Copyright 2001 by Arnaud Brejeon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid