Vishnu Lal
Vishnu Lal

Reputation: 189

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 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>

Upvotes: 1

Views: 3711

Answers (1)

Steve Townsend
Steve Townsend

Reputation: 54148

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.

Upvotes: 1

Related Questions