如何使用boost的ptree删除xml属性 title ? 我有一个xml,我尝试按照代码删除属性 title 并保存到新的xml但失败了(new.xml仍然有title属性)。
的xml:
<?xml version="1.0" encoding="utf-8"?>
<tokens title="issues"></tokens>
代码:
ptree pt;
read_xml("C://old.xml", pt);
pt.erase("tokens.<xmlattr>.title"); //try one
pt.erase("tokens.<xmlattr>"); //try two
write_xml("C://new.xml", pt);
是否有任何方法可以使用boost ptree删除属性?
答案 0 :(得分:2)
您可以尝试这样的事情(正确的错误处理):
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/exceptions.hpp>
#include <sstream>
#include <iostream>
using namespace boost;
using namespace boost::property_tree;
using namespace std;
int main()
{
ptree pt;
{
std::stringstream strm;
strm << "<?xml version='1.0' encoding='utf-8'?> <tokens title='issues'></tokens>";
read_xml(strm, pt);
}
pt.find("tokens")->second.erase("<xmlattr>");
write_xml(cout, pt, xml_writer_make_settings(' ', 2));
}
输出:
$ ./test
<?xml version="1.0" encoding="utf-8"?>
<tokens/>