Poco XMLconfiguration 解析xml配置文件

 

環境: Centos7ios

GCC: 7.3.0spa

 

準備須要讀取的xml文件:code

<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.ccxml

#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; }

 

編譯代碼:blog

相關文章
相關標籤/搜索