我想创建使用 POCO 库读写 xml 的 C++ 程序。
所以,我写了一个简单的程序。
我可以读取每个 xml 元素。
但我无法编写新的 xml 元素。
下面是test.xml。
<config>
<prop1>value1</prop1>
</config>
这是我的代码。
#include <iostream>
#include <string>
#include <Poco/AutoPtr.h>
#include <Poco/Util/XMLConfiguration.h>
using Poco::AutoPtr;
using Poco::Util::XMLConfiguration;
int main(int argc, char const* argv[])
{
AutoPtr<XMLConfiguration> pConf(new XMLConfiguration("test.xml"));
std::string prop1 = pConf->getString("prop1");
std::cout << "prop1 = " << prop1 << std::endl;
pConf->setString("prop2", "input");
pConf->save("test.xml");
return 0;
}
程序运行后,我希望 xml 文件更改如下。
<config>
<prop1>value1</prop1>
<prop2>input</prop2> <--New element added!!
</config>
但是结果如下。
<config>
<prop1>value1</prop1>
</config>
没有任何改变...
我做错了什么?
提前致谢。
添加信息如下
- 操作系统::Linux Mint18.1
- POCO 库版本::1.7.6(从源代码构建)
- 构建系统::CMake、make 和 GCC
我运行以下命令。
sudo apt-get remove ros-kinetic-desktop-full
然后,我可以使用上面的程序编写 xml 文件。
我认为 ros 使用 POCO 库,它与我的 POCO 库重复。
所以我尝试将 POCO library(ros) 替换为 POCO library(mine)。
我的尝试很顺利!
最佳答案
解决方法如下
如果你可以读取 xml 文件而不能使用 POCO 库编写 xml 文件。
您的 POCO 库和 ROS 的 POCO 库可能重复。
尝试以下命令。
安装ROS
sudo apt-get install ros-kinetic-desktop-full
将带有目标前缀 的 Poco 库重新安装到/usr/lib 和/usr/include
./configure --prefix=/usr
制作
sudo 进行安装
关于c++ - 如何在ROS环境下用POCO库写XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42561010/