how to create xml using boost property_tree

久未见 提交于 2019-12-24 10:59:07

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/15413022/how-to-create-xml-using-boost-property-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!