如何从Boost.PropertyTree复制子树 [英] How to copy subtree from Boost.PropertyTree
本文介绍了如何从Boost.PropertyTree复制子树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些 boost :: property_tree :: ptree
。我需要删除带有某些标签名称的某些元素的树。例如,源 ptree
的xml如下:
I have some boost::property_tree::ptree
. I need tree with removing some elements with certain tag name. For example, xml for source ptree
is the following:
<?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如下所示:
And I'd like to get ptree
with xml like the following:
<?xml version="1.0" encoding="utf-8"?>
<document>
<B atr="one" atr1="something" />
<E>nothing</E>
</document>
如何编写函数,生成新的 ptree
已删除< to_remove>
节点?
How to write function, that generate new ptree
with removed <to_remove>
nodes?
推荐答案
ptree是std :: pair< const Key,self_type>,因此您可以迭代树并删除相应的节点。以下是一个示例。
The value_type of ptree is std::pair< const Key, self_type >, so you can iterate the tree and remove corresponding nodes. The following is a sample.
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;
}
}
}
这篇关于如何从Boost.PropertyTree复制子树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文