【发布时间】:2015-02-06 18:20:19
【问题描述】:
我有一段代码可以迭代提升属性树 (XML)。
我需要当前节点的 ptree,而不是节点的子节点。
更新
xml树
<node id="A.html">
<subnode> child A1 </subnode>
<subnode> child A2 </subnode>
</node>
<node id="B.html">
<subnode> child B1 </subnode>
<subnode> child B2 </subnode>
</node>
迭代代码
void parse_tree(ptree& pt, std::string key)
{
string nkey;
if (!key.empty())
nkey = key + ".";
ptree::const_iterator end = pt.end();
for(ptree::iterator it = pt.begin(); it != end; ++it){
//if the node's id is a .html filname, save the node to file
string id = it->second.get("<xmlattr>.id","");
if(id.find("B.html") != std::string::npos){ //Let's just test for "B.html"
write_xml("test.html", pt); //saves entire tree
write_xml("test.html", it->second); //saves only children of the node
}
parse_tree(it->second, nkey + it->first); //recursion
}
}
使用 write_xml("test.html", pt)的结果
(我们得到整棵树,我们只想要节点)
<node id="A.html">
<subnode> child A1 </subnode>
<subnode> child A2 </subnode>
</node>
<node id="B.html">
<subnode> child B1 </subnode>
<subnode> child B2 </subnode>
</node>
使用 write_xml("test.html", it->second) 的结果
(我们没有父节点..只有子节点)
<subnode> child B1 </subnode>
<subnode> child B2 </subnode>
想要的结果
(我们想要节点,它是孩子,.. 像这样)
<node id="B.html">
<subnode> child B1 </subnode>
<subnode> child B2 </subnode>
</node>
【问题讨论】:
标签: c++ xml boost tree boost-propertytree