Poco XMLconfiguration 解析xml配置文件

环境: Centos7

GCC: 7.3.0

准备需要读取的xml文件:

<config>
    <prop1>1.23</prop1>
    <prop2>2.34</prop2>
    <prop3>
        <prop4 attr="1"/>
        <prop4 attr="2"/>
    </prop3>
    <prop5 id="first">hello,</prop5>
    <prop5 id="second"> world!</prop5>
</config>

创建需要解析的xml_config.cc

#include<Poco/Util/AbstractConfiguration.h>
#include<Poco/Util/XMLConfiguration.h>
#include<iostream>

using namespace std;
using namespace Poco::Util;

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;
    return 0;
}

编译代码:

原文地址:https://www.cnblogs.com/Davirain/p/11591997.html