如何使用升压property_tree创建XML [英] how to create xml using boost property_tree

查看:73
本文介绍了如何使用升压property_tree创建XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是

 <应答GT;
      <&指标GT;
          <索引>&ABC LT; /索引>
          <索引>&XYZ LT; /索引>
          <索引>&PQR LT; /索引>
      < /索引>
< /响应>

我有名单在我的矢量index_list。

任何一个可以帮助我。

我已经尝试了一些code表示。
随后

 的boost :: property_tree :: ptree中树;
stringstream的输出;
对于(的std ::矢量<串GT; ::为const_iterator吧= index_list.begin();
        !它= index_list.end();它++){
    性病::法院LT&;< *它<< \\ n;
    tree.put(response.indexes.index,*吧);
}
如果(格式==XML){
    write_xml(输出树);
}其他{
    write_json(输出树);
}

当我运行上面的code。 I M在列表中只得到姓氏。这是

 <应答GT;
  <&指标GT;
      <索引>&PQR LT; /索引>
  < /索引>
< /响应>


解决方案

方法将清除任何现有值,请参阅前面的问题,<一个href=\"http://stackoverflow.com/questions/13072704/boost-property-tree-multiple-values-per-key\">here这是相关的。

您将不得不使用列表中的每个条目不同的密钥对你的逻辑,以避免数据丢失。

加速文档说。


  

调用看跌将在指定的路径中插入一个新的值,以便一个呼叫
  获得指定相同的路径检索。


i need to create xml for my output. I have a list of index names. i want to populate it in an xml file in one format.

that is

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

I have the list in my vector index_list.

Can any one help me out.

I have tried some code for that. which follows

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);
}

When i run the above code . i m getting only last name in the list. that is

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

解决方案

The put method will erase any existing value, see the earlier question here that's related.

You'll have to use different keys for each entry in the list for your logic to avoid data loss.

Boost docs say

Calling put will insert a new value at specified path, so that a call to get specifying the same path will retrieve it.

这篇关于如何使用升压property_tree创建XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆