Adding nodes with the same key to the property tree

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?

+6
source share
1 answer

The add_child member add_child allows add_child to insert one property_tree into the DOM of another as a child node. If the key path you specified already exists, a duplicate key will be added and a child will be inserted instead. If we change your example a little, we can check the results.

 #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> int main() { // Create the first tree with two elements, name and contents boost::property_tree::ptree ptr1; ptr1.put("name", "a2"); ptr1.put("contents", "adsf"); // Create the a second tree with two elements, name and contents boost::property_tree::ptree ptr2; ptr2.put("name", "d6"); ptr2.put("contents", "345"); // Add both trees to a third and place them in node "spreadsheet.cell" boost::property_tree::ptree ptr3; ptr3.add_child("spreadsheet.cell", ptr1); ptr3.add_child("spreadsheet.cell", ptr2); boost::property_tree::write_xml("output.xml", ptr3); return 0; } 

When you call add_child for the first time, the node for the key "spreadsheet.cell" does not exist and is not created. Then it adds the contents of the tree ( name and contents ) to the newly created node. When you call add_child a second time, it sees that "spreadsheet.cell" already exists, but unlike put it creates a child element of the node, also called a "cell", and inserts it in the same place.

Final result:

 <?xml version="1.0" encoding="utf-8"?> <spreadsheet> <cell> <name>a2</name> <contents>adsf</contents> </cell> <cell> <name>d6</name> <contents>345</contents> </cell> </spreadsheet> 
+15
source

All Articles