原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