您的位置:首页 > 编程语言 > C语言/C++

Poco读写XML配置文件示例

2015-04-07 14:33 489 查看
原XML配置文件:

<root>
<hostname>ZHANGSAN</hostname>
<ip>127.0.0.1</ip>
</root>


执行后XML配置文件:

<root>
<hostname>ZHANGSAN</hostname>
<ip>127.0.0.1</ip>
<port>1521</port>
</root>


Poco读取修改配置文件代码:

#include <Poco/AutoPtr.h>
#include <Poco/Util/XMLConfiguration.h>

std::string fileName = "D:\\test.xml";
Poco::AutoPtr<Poco::Util::XMLConfiguration> pXML(new Poco::Util::XMLConfiguration(fileName));
std::string hostname = pXML->getString("hostname");
std::string ip = pXML->getString("ip");

cout<<"hostname:"<<hostname<<"\nip:"<<ip<<endl;

pXML->setInt("port", 1521);
pXML->save(fileName);

XML文件格式:

<config>
<prop1>value1</prop1>
<prop2>value2</prop2>
<prop3>
<prop4 attr="value3"/>
<prop4 attr="value4"/>
</prop3>
<prop5 id="first">value5</prop5>
<prop5 id="second">value6</prop5>
</config>

读取属性时的字符串:
prop1 -> value1

prop2 -> value2

prop3.prop4 -> (empty string)

prop3.prop4[@attr] -> value3

prop3.prop4[1][@attr] -> value4

prop5[0] -> value5

prop5[1] -> value6

prop5[@id=first] -> value5

prop5[@id='second'] -> value6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ Poco xml