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>
2
  • This works. I don't have any experience with this library so I don't know if it's the best way to do it. Commented Mar 14, 2013 at 16:07
  • thanks llonesmiz.. Your way of doing is also good.. impressed. Commented Mar 14, 2013 at 16:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your info. if i use put i cant get xml like <indexes><index/><index/></indexes>.. i will try add()..
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.