Click here to Skip to main content
14,207,916 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 12 Sep 2011
Licenced CC (ASA 3U)

Learning Poco: Load XML Configuration

, 12 Sep 2011
Rate this:
Please Sign up or sign in to vote.
In this tutorial, we will load XML configuration from files ystem by using XMLConfiguration, and access specific configuration items by using methods provided by AbstractConfiguration.

In this tutorial, we will load an XML configuration from the file system by using XMLConfiguration, and access specific configuration items by using methods provided by AbstractConfiguration. Here, AbstractConfiguration is an interface, and XMLConfiguration is the implementation, both provided by Poco.

Prepare an XML configuration file called conf.xml like this:

       <prop4 attr="1"/>
       <prop4 attr="2"/>
    <prop5 id="first">Hello,</prop5>
    <prop5 id="second"> world!</prop5>

Then create the source file

#include <span class="code-keyword"><Poco/Util/AbstractConfiguration.h>
</span>#include <span class="code-keyword"><Poco/Util/XMLConfiguration.h>
</span>#include <span class="code-keyword"><iostream>
using namespace Poco::Util;
using namespace std;

int main()
  AbstractConfiguration *cfg = new XMLConfiguration("conf.xml");

  double prop1 = cfg->getDouble("prop1");
  double prop2 = cfg->getDouble("prop2");
  cout << "prop1 + prop2 = " << prop1 + prop2 << endl;

  cout << "This is an empty string: " 
       << cfg->getString("prop3.prop4") << endl;

  int prop4   = cfg->getInt("prop3.prop4[@attr]");
  int prop4_0 = cfg->getInt("prop3.prop4[0][@attr]");
  int prop4_1 = cfg->getInt("prop3.prop4[1][@attr]");
  cout << "prop4 + prop4_0 + prop4_1 = " 
       << prop4 + prop4_0 + prop4_1 << endl;

  cout << cfg->getString("prop5[0]") 
       << cfg->getString("prop5[1]") << endl;
  cout << cfg->getString("prop5[@id=first]") 
       << cfg->getString("prop5[@id='second'") << endl;

  // No need to delete cfg, since it is reference counted for garbage collection

  return 0;

Compile it and run:

$ g++ -o xml_conf -lPocoUtil
$ ./xml_conf
prop1 + prop2 = 3.57
This is an empty string: 
prop4 + prop4_0 + prop4_1 = 4
Hello, world!
Hello, world!

That’s it!

The code above is very easy to understand, and notice that there is no need to delete cfg, since it is reference counted for garbage collection and will be deleted by Poco automatically. And you can not write like the following because the destructor of both AbstractConfiguration and XMLConfiguration are protected.

// You can NOT write like this! It won't compile!
XMLConfiguration config("conf.xml");

So refactor your code and use it now! XML configuration is much more flexible than plain-text files, and Poco handles everything you need to work with a configuration file. Why not give it a try?


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


About the Author

Student Shanghai Jiao Tong University
China China
No Biography provided

Comments and Discussions

Bug[My vote of 2] The use of Poco::AbstractConfiguration is incorrect. Pin
John Toops18-Jan-16 11:31
memberJohn Toops18-Jan-16 11:31 
Hi, there are several issues with the posted code. I am just clearing up a few misunderstandings.

1) "Poco::AbstractConfiguration *cfg" - This syntax creates a pointer to an object. Unless it is manually deleted, it will leak memory.

2) Poco::AbstractConfiguration derives from RefCountedObject. RefCountedObject is not the same as a CountedPointer. A RefCountedObject is used by a CountedPointer, or in this case, by Poco::AutoPtr.

3) XMLConfiguration is a derived class of AbstractConfiguration. As such, leaving it as an XMLConfiguration object gives one access to its extra methods, if any.

So, it just should look like:

AutoPtr<xmlconfiguration> cfg = new XMLConfiguration("conf.xml");
GeneralMy vote of 4 Pin
roman.jetich13-Dec-12 9:07
memberroman.jetich13-Dec-12 9:07 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190612.1 | Last Updated 12 Sep 2011
Article Copyright 2011 by stfairy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid