[英]How to RESET attribute of xml element using boost::property_tree?
I have a xml file, I need to modify few attributes in that file. 我有一个xml文件,我需要修改该文件中的一些属性。
My xml file is something like below: 我的xml文件如下所示:
<ns0:App xmlns:ns0="AppSchema" MyDir1="C:\App\Dir1" MyDir2="C:\App\Dir2" ..... some other attributes>
<ns0:Backend DisableBackend="false">
<ns0:Logging EnableLogging="false" LogPath="c:\mylogs"/>
<ns0:ExternalTool EnableTool="false"/>
</ns0:Backend>
</ns0:App>
I need to modify values of attributes like MyDir1, MyDir2, EnableLogging, EnableTool in this xml file. 我需要在此xml文件中修改MyDir1,MyDir2,EnableLogging,EnableTool等属性的值。
Googling helped me to get value from the attribute, but no luck in - how to modify attribute value or may be I am failing to use correct syntax. 谷歌搜索帮助我从属性中获取值,但是没有运气-如何修改属性值,或者可能是我没有使用正确的语法。
Could anybody guide me in this ? 有人可以指导我吗?
The code that I tried is as below: 我尝试过的代码如下:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace boost::property_tree;
int main()
{
std::string xml_original = "C:\\temp\\config.xml";
std::string xml_updated = "C:\\temp\\config_updated.xml";
ptree tree;
read_xml(sysCfg, tree);
std::cout<<"old value: " << tree.get<std::string>("App.<xmlattr>.MyDir1");
tree.put("App.<xmlattr>.MyDir1", "newPath");
xml_writer_settings<char> w(' ', 4);
write_xml(xml_updated, tree, std::locale(), w);
}
You have to include the namespace. 您必须包括名称空间。 I also used
std::string
for the xml_writer_settings
: 我还对
xml_writer_settings
使用了std::string
:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using namespace boost::property_tree;
int main()
{
std::string xml_original = "config.xml";
std::string xml_updated = "config_updated.xml";
ptree tree;
read_xml(xml_original, tree);
// must include the ns0 namespace
std::cout << "old value: " << tree.get<std::string>("ns0:App.<xmlattr>.MyDir1");
tree.put("ns0:App.<xmlattr>.MyDir1", "newPath");
xml_writer_settings<std::string> w(' ', 4);
write_xml(xml_updated, tree, std::locale(), w);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.