- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段代码可以遍历 boost 属性树 (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>
最佳答案
更新 2
为回应评论/更新的问题而重写。
有两种方法。
您可以使用未记录的函数 write_xml_element
编写单个元素(使用键作为元素名称):
// write the single element: (undocumented API)
boost::property_tree::xml_parser::write_xml_element(
std::cout, it->first, it->second,
0, settings
);
或者您可以使用单个 child 创建一个新的ptree
对象
ptree tmp;
tmp.add_child(it->first, it->second);
write_xml(std::cout, tmp, settings);
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <fstream>
#include <iostream>
using namespace boost::property_tree;
void parse_tree(ptree& pt, std::string key)
{
std::string nkey;
auto settings = xml_parser::xml_writer_make_settings<std::string>('\t', 1);
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 an .html filname, save the node to file
std::string id = it->second.get("<xmlattr>.id","");
if (id.find(key) != std::string::npos) {
// write the single element: (undocumented API)
boost::property_tree::xml_parser::write_xml_element(
std::cout, it->first, it->second,
0, settings
);
// or: create a new pt with the single child
std::cout << "\n==========================\n\n";
ptree tmp;
tmp.add_child(it->first, it->second);
write_xml(std::cout, tmp, settings);
}
parse_tree(it->second, nkey + it->first); //recursion
}
}
int main() {
ptree pt;
read_xml("input.txt", pt);
parse_tree(pt, "B");
}
输出:
<node id="B.html">
<subnode> child B1 </subnode>
<subnode> child B2 </subnode>
</node>
==========================
<?xml version="1.0" encoding="utf-8"?>
<node id="B.html">
<subnode> child B1 </subnode>
<subnode> child B2 </subnode>
</node>
关于c++ - 从 boost::property_tree::ptree::iterator 获取 ptree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28372268/
我需要在我的程序中使用 boost::property_tree。现在我很难弄清楚如何将它与自定义路径类型一起使用。我想要的路径类型是 Framework::CommonClientServer::I
如何在循环数组时向属性树添加节点? 这可能是一件简单的事情,但我只是不明白如何使用 boost_property_tree。 我有一组值,我想将其添加到属性树并将其输出为 xml。如果我对节点进行硬编
请考虑两个“boost::property_tree”-s。 ptree1: { "node1" : 1, "node_that_only_appears_in_this_one" :
我正在使用 boost::property_tree 从/向文件加载/保存一组参数。其中许多参数是枚举(不同类型)。所以我需要一种从 boost::property_tree 获取枚举的方法(即将字符
我想使用 boost property_tree 解析下面的 xml 结构。 我能够解析上面 xml 的第一
是否可以在 boost 属性树中存储指向对象的指针,然后使用迭代器检索数据?我正在尝试做类似的事情: property_tree::ptree pt; pt.put("1.2.3.4", new My
感谢 Boost::Property_Tree,我正在尝试配置我的 XML 文件。我想要的聊天是: 10 我所拥有的是: first>10 我发现我不得不使用 boost::property_
我需要从文件中读取属性以影响程序行为。看起来 boost::property_tree 会做得很好。但是,我想知道在获取不同类型的值时,库是否可能会多次读取文件? 出于性能原因,我希望它只有一次。大多
我正在加载一个带有 boost property_tree 的 ini 文件。我的 ini 文件主要包含“简单”类型(即字符串、整数、 double 等),但我确实有一些值表示数组。 [Example
我的 JSON 是这样的: { "apps":[ { "id":"x", "val":"y", } ]
我发现 boost::property_tree::ptree 有巨大的内存开销。我的估计是一个空的 ptree 大约有 150 个字节,并且,放入 ptree 中的任何条目至少增加了 150 个字节
我想要的是这样的: Mr. Heik 37/100
我正在读取 xml 文件并存储到 boost::property_tree::ptree pt(object) 中。 我正在尝试使用在 ptree_serialization.hpp 中定义的方法 s
考虑以下示例: #include #include typedef boost::property_tree::ptree PT; struct Foo { int bar; in
假设我有两个 XML: foo foo 有没有办法在不指定绝对路径的情况下使用属性树从两个 xml 中提取 my_value? 目前我能做的最好的是: std::string first("some.
我正在类成员函数中执行 ptree.put()。 class Writer{ private: boost::property_tree::ptree ptree_; public:
我正在尝试使用显示的方法从 boost::property_tree 中读取数组数据 in this question .在该示例中,数组首先作为字符串读取,转换为字符串流,然后读入数组。在实现该解决
我正在寻找与 boost::property_tree 非常相似的结构.但是它应该更加类型安全,例如我想在我这样做时得到一个异常(exception): #include int main() {
我需要解析一些 INI 文件。为此,我尝试使用 boost::property_tree,但在我的系统中不允许出现异常。 如何在使用 boost::property_tree 时禁用异常支持? 如果没
如何从 boost::property_tree 中获取枚举? 这是我的“非工作”示例。 配置文件 EMISSION::EMIT1 42 main.cpp #include #inclu
我是一名优秀的程序员,十分优秀!