1

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>
Vishnu Lal
  • 189
  • 1
  • 4
  • 13
  • [This](http://liveworkspace.org/code/2bMAyG$0) works. I don't have any experience with this library so I don't know if it's the best way to do it. –  Mar 14 '13 at 16:07
  • thanks llonesmiz.. Your way of doing is also good.. impressed. – Vishnu Lal Mar 14 '13 at 16:58

1 Answers1

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.

Community
  • 1
  • 1
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • thanks for your info. if i use put i cant get xml like .. i will try add().. – Vishnu Lal Mar 14 '13 at 15:38
  • My impression is that this is a feature of propertytree. XML is just used to represent the data structure, so don't expect to do everything with PTree that you can do in XML. – Steve Townsend Mar 14 '13 at 15:42