我有一些 boost::property_tree::ptree
.我需要删除一些具有特定标签名称的元素的树。例如,源的 xml ptree
是以下内容:
<?xml version="1.0" encoding="utf-8"?>
<document>
<B atr="one" atr1="something">
<to_remove attr="two">10</to_remove>
</B>
<to_remove>
<C>value</C>
<D>other value</D>
</to_remove>
<E>nothing</E>
</document>
我想得到 ptree
使用如下 xml:
<?xml version="1.0" encoding="utf-8"?>
<document>
<B atr="one" atr1="something" />
<E>nothing</E>
</document>
如何编写生成新的函数ptree
删除了 <to_remove>
节点?
最佳答案
ptree的value_type是std::pair< const Key, self_type >,所以可以迭代树,去掉对应的节点。以下是示例。
void remove(ptree &pt){
using namespace boost::property_tree;
for (auto p = pt.begin(); p != pt.end();){
if (p->first == "to_remove"){
p = pt.erase(p);
}
else{
remove(p->second);
++p;
}
}
}
关于c++ - 如何从 Boost.PropertyTree 复制子树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24235589/