Updating XML file using Boost property_tree

I have the following XML file:

<xml version="1.0" encoding="utf-8"?>

I want to add a new node: Parameter2="2" to the Data node. This code doesn't work, saved file still contains only one parameter:

    boost::property_tree::ptree tree;
    boost::property_tree::ptree dataTree;

    read_xml("test.xml", tree);
    dataTree = tree.get_child("Data");
    dataTree.put("Parameter2", "2");

    boost::property_tree::xml_writer_settings w(' ', 4);
    write_xml("test.xml", tree, std::locale(), w);

If I add these two lines after dataTree.put, I get correct result:

    tree.add_child("Data", dataTree);

I don't like this solution, because it creates problems with more complicated tree structutes. Is it possible to update property tree without deleting/adding child nodes?

Read more here: https://stackoverflow.com/questions/3299126/updating-xml-file-using-boost-property-tree

Content Attribution

This content was originally published by Alex F at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: