I am using Boost's property tree to read and write XML. Using a spreadsheet application I made I want to save the contents of the spreadsheet to xml. This is a school assignment so I am required to use the following format for the 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 the put
method replaces anything at that node, instead of adding a new one. Which is exactly the functionality I am seeing:
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 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 quite figure out how to use that add_child
method, could someone explain how to use it?
Is there a better way of going about this to achieve the file format I want?
"spreadsheet.cell.d6"
– Cockloft