Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / XML
Article

Read and Write application parameters in XML

Rate me:
Please Sign up or sign in to vote.
4.38/5 (33 votes)
30 Jun 20034 min read 1.6M   6.8K   141   175
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

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


Written By
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

 
GeneralUNICODE -- Very Important Pin
ExtraLean20-Feb-04 8:30
ExtraLean20-Feb-04 8:30 
GeneralRe: UNICODE -- Very Important Pin
ExtraLean20-Feb-04 9:03
ExtraLean20-Feb-04 9:03 
GeneralRe: UNICODE -- Very Important Pin
Arnaud Brejeon20-Feb-04 21:53
Arnaud Brejeon20-Feb-04 21:53 
QuestionHow to change the attributes of xml Pin
Boggarapu Sateesh Kumar14-Feb-04 2:01
Boggarapu Sateesh Kumar14-Feb-04 2:01 
GeneralIm having problems Pin
Tauri29-Jan-04 6:37
Tauri29-Jan-04 6:37 
GeneralRe: Im having problems Pin
Nitron19-Feb-04 9:47
Nitron19-Feb-04 9:47 
GeneralPerfect! Pin
Nitron7-Jan-04 7:13
Nitron7-Jan-04 7:13 
GeneralCannot compile in VC++6 - returns 252 errors! Pin
Andrew Fox24-Dec-03 2:50
Andrew Fox24-Dec-03 2:50 
Hi,
I have read whole discussion about this library, but I cannot solve my problem. I wrote to all .cpp files the stdafx.h and set to static library. But during compilation, it returns 252 errors. The first & the most important(I think) is in ParamIO.cpp: ParamIO is not a class or a namespace name. All other errors are depending on this one. I don't know what to do...
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Arnaud Brejeon24-Dec-03 4:43
Arnaud Brejeon24-Dec-03 4:43 
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Andrew Fox24-Dec-03 5:15
Andrew Fox24-Dec-03 5:15 
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Arnaud Brejeon27-Dec-03 22:17
Arnaud Brejeon27-Dec-03 22:17 
GeneralRe: Cannot compile in VC++6 - returns 252 errors! Pin
Andrew Fox27-Dec-03 22:53
Andrew Fox27-Dec-03 22:53 
GeneralThis won't compile with Visual Studio .NET 2003 Pin
TheBunyip19-Aug-03 4:40
TheBunyip19-Aug-03 4:40 
GeneralRe: This won't compile with Visual Studio .NET 2003 Pin
Bobby Mihalca19-Dec-03 5:43
Bobby Mihalca19-Dec-03 5:43 
GeneralRe: This won't compile with Visual Studio .NET 2003 Pin
Bobik26-Jul-04 4:10
Bobik26-Jul-04 4:10 
GeneralRe: This won't compile with Visual Studio .NET 2003 Pin
buntiman2-Oct-04 3:23
buntiman2-Oct-04 3:23 
GeneralRe: This won't compile with Visual Studio .NET 2003 Pin
tjroamer1-Jun-08 2:30
tjroamer1-Jun-08 2:30 
Generalmultiple entries of the same name Pin
burnettb31714-Jul-03 11:18
burnettb31714-Jul-03 11:18 
GeneralRe: multiple entries of the same name Pin
disabledman24-Mar-04 21:00
disabledman24-Mar-04 21:00 
GeneralExtension to read/write integer and double arrays Pin
DWelter29-Jun-03 23:53
DWelter29-Jun-03 23:53 
GeneralUpdate Pin
Arnaud Brejeon19-Jun-03 15:49
Arnaud Brejeon19-Jun-03 15:49 
GeneralRe: Update Pin
Vladimir G. Ivanov27-Jun-03 10:36
Vladimir G. Ivanov27-Jun-03 10:36 
GeneralRe: Update Pin
VinZ29-Jun-03 22:00
VinZ29-Jun-03 22:00 
GeneralRe: Update Pin
Ogi30-Jun-03 4:57
Ogi30-Jun-03 4:57 
GeneralRe: Update Pin
shutao5-Aug-03 5:23
shutao5-Aug-03 5:23 

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.