How to create xml using boost property_tree

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

i.e

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

I have a list in my index_list.

Can someone help me.

I tried some code for this. what 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 only get the last name on the list. i.e

 <response> <indexes> <index>pqr</index> </indexes> </response> 
+1
source share
1 answer

The put method will destroy any existing value; see the previous question here which is related.

You will need to use different keys for each entry in the list for your logic to avoid data loss.

Boost docs say

Calling put will add a new value at the specified path, so that the call will receive it in order to get an indication of the same path.

+1
source

All Articles