You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Lars Uffmann <la...@dlr.de> on 2008/06/16 19:02:19 UTC

storing application configuration data in XML files

Hi everyone,

Have you ever been wringing your hands over having to handle the 
configuration file for yet another C++ application? Where you want the 
config file to be human readable, writeable, and input-tolerant, while 
at the same time do not want to do all the error checking yourself?

I've been busy the past weeks to get to know some things in C++ that I 
hadn't worked much with before, and finally using Xerces-C, if only to a 
minimal extent. The idea was to use XML for configuration files, so that 
the XML parser (in this case Xerces-C) does the work of validating the 
correct input scheme for me. I've done this with ASP and PHP in the past 
and like the concept. Now I wanted something similar for C++.

The result is the attached xmlconf-tool - a simple header-file and it's 
according cpp-file. Should be self explaining, documentatory example is 
in the header file.


A typical config file written by xmlconf would look like this:
-------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
	<ApplicationSettings>
		<udp_port type="int" value="6000" />
		<listen_on_startup type="bool" value=" 1" />
		<default_directory type="string" value="." />
	</ApplicationSettings>
	<WindowSettings>
		<width type="uint" value="234" />
		<heigth type="uint" value="234" />
	</WindowSettings>
</root>
-------

Quoting from xmlconf.h:
-------
Features:
  - parameter categories in XML file (1 level when saving, more accepted
    but ignored when reading)
  - support for: bool, int, unsigned int, long, unsigned long and string
    parameters
  - throws std::runtime_error exceptions, with a short explanation in
    .what
  - "safe" file writing: first write to a temp file, then delete old
    file, then overwrite from temp file
  - easy handling of multiple configuration files - just create a new
    instance
  - no source code editing required, simply access your parameters at
    runtime by the names & types that YOU know they have, e.g.
    config["myParam"].as_int (if .type is t_int)
  - alphabetical sorting of categories and parameters when saving
    (built-in C++ map sorting)

Restrictions:
  - since categories and parameters are stored as XML nodes, they must
    abide the rules for XML node names (for example: no blankspaces)
  - Xerces-C library support is required

Not Supported (yet): floating point parameters (easy to add though)
-------

I'm happy for any feedback - I surely made some mistakes. So let me know 
what you think!

Best Regards,

    Lars

-- 
Dipl.-Ing. Lars Uffmann
Microgravity User Support Center (MUSC)
Deutsches Zentrum für Luft- und Raumfahrt e.V.
Linder Höhe
D-51147 Köln
phone: +49 (0)2203 601 2171
http://www.dlr.de/musc

Re: storing application configuration data in XML files

Posted by Lars Uffmann <la...@dlr.de>.
Sorry - forgot an include that is no longer needed - you can delete the 
following line from xmlconf.cpp:

#include <boost/scoped_array.hpp>