I use the Boost property tree to read and write XML. Using a spreadsheet application, I did to save the contents of the table in xml. This is a school assignment, so I have to use the following format for XML:
<?xml version="1.0" encoding="UTF-8"?> <spreadsheet> <cell> <name>A2</name> <contents>adsf</contents> </cell> <cell> <name>D6</name> <contents>345</contents> </cell> <cell> <name>D2</name> <contents>=d6</contents> </cell> </spreadsheet>
For a simple test program, I wrote:
int main(int argc, char const *argv[]) { boost::property_tree::ptree pt; pt.put("spreadsheet.cell.name", "a2"); pt.put("spreadsheet.cell.contents", "adsf"); write_xml("output.xml", pt); boost::property_tree::ptree ptr; read_xml("output.xml", ptr); ptr.put("spreadsheet.cell.name", "d6"); ptr.put("spreadsheet.cell.contents", "345"); ptr.put("spreadsheet.cell.name", "d2"); ptr.put("spreadsheet.cell.contents", "=d6"); write_xml("output2.xml", ptr); return 0; }
Based on this question , I see that the put method replaces something on this node instead of adding a new one. These are exactly the functions that I see:
Output.xml
<?xml version="1.0" encoding="utf-8"?> <spreadsheet> <cell> <name>a2</name> <contents>adsf</contents> </cell> </spreadsheet>
Output2.xml
<?xml version="1.0" encoding="utf-8"?> <spreadsheet> <cell> <name>d2</name> <contents>=d6</contents> </cell> </spreadsheet>
Looking at the documentation, I see this add_child method, which will be Add the node at the given path. Create any missing parents. If there already is a node at the path, add another one with the same key. Add the node at the given path. Create any missing parents. If there already is a node at the path, add another one with the same key.
I can't figure out how to use this add_child method, can someone explain how to use it?
Is there a better way to achieve this in order to achieve the desired file format?