如何使用boost property_tree创建XML

how to create xml using boost property_tree

本文关键字:tree 创建 XML property 何使用 boost      更新时间:2023-10-16

我需要为我的输出创建XML。我有一个索引名列表。我想用一种格式将它填充到XML文件中。

<response>
      <indexes>
          <index>abc</index>
          <index>xyz</index>
          <index>pqr</index>
      </indexes>
</response>

我在vector index_list中有一个列表。

有谁能帮我一下吗?

我已经尝试了一些代码。它遵循

boost::property_tree::ptree tree;
stringstream output;
for (std::vector<string>::const_iterator it = index_list.begin();
        it != index_list.end(); it++) {
    std::cout << *it << "n";
    tree.put("response.indexes.index", *it);
}
if (format == "xml") {
    write_xml(output, tree);
} else {
    write_json(output, tree);
}

当我运行上面的代码。我只知道名单上的姓。这就是

<response>
  <indexes>
      <index>pqr</index>
  </indexes>
</response>

put方法将擦除任何现有值,请参阅此处先前的相关问题。

你必须为你的逻辑列表中的每个条目使用不同的键,以避免数据丢失。

Boost docs说

调用put将在指定路径插入一个新值,以便调用如果指定相同的路径,将检索它。