Poco读写XML配置文件示例
2015-04-07 14:33
489 查看
原XML配置文件:
执行后XML配置文件:
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
<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
相关文章推荐
- 使用XML读写删除功能来实现资源文件配置
- java读写xml和properties配置文件and Properties支持中文(转)
- java使用dom4j解析xml配置文件实现抽象工厂反射示例
- C++对ini配置文件与xml配置文件的读写
- Spring XML配置文件示例(二)——web.xml
- [原创]简单的XML配置文件读写类
- c#读写ini配置文件示例
- VC使用tinyxml读写INI,cfg,XML配置文件
- UWSGI配置文件---ini和xml示例
- WinMoblie 利用EVC读写INI,XML配置文件
- 一个将配置文件转换成xml的示例程序
- VB scrpit 修改配置文件(读写XML)
- UWSGI配置文件---ini和xml示例
- 用java读写ini配置文件的原因以及实现 示例
- VB中INI配置文件读写示例
- C# 读写配置和xml文件
- 二、添加IP配置对话框,读写XML文件
- VC使用tinyxml读写INI,cfg,XML配置文件
- Spring XML配置文件示例(一)——<Servlet name>-servlet.xml
- 读写config,xml配置文件