1

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

那是

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

我的向量 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>
4

1 回答 1

1

put方法将删除任何现有值,请参阅此处相关的较早问题。

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

Boost 文档

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

于 2013-03-14T15:35:05.290 回答