How do you add nodes to a property tree while looping a array?
This is probably a simple thing but I just don't understand how to work with boost_property_tree.
I have an array of values that I want to add to a property tree and output it as xml. If I hardcode the nodes and add them by hand to root node it works but if I try to do it from inside a loop the xml is invalid, contains only one node from the array and none of the xml nodes are terminate.
It's a variable length array of data so hardcoding the nodes are not possible. Doing something like this simply doesn't work. And I don't understand why?
ptree listnode;
std::vector<data> dataarray= ...
for(auto data : dataarray)
{
ptree node;
...
listnode.add_child("value", node)
}
...
What I want is something like this:
<list>
<value active="true">12</value>
<value active="true">44</value>
<value active="true">23</value>
</list>
But the resulting xml looks like this:
<list>
<value active="true">
It just stops after the first value node. No exceptions or anything so I'm assuming that either my property_tree is invalid or its the xml_parser::write_xml that fails to create the xml.
Most likely I have misunderstood how to use boost::property_tree. But really, outputting a list must be a pretty simple thing?
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using namespace boost::property_tree;
int main() {
ptree pt;
auto& list = pt.add_child("list", ptree{});
for (auto data : { 12, 44, 23 })
list.add("value", data)
.add("<xmlattr>.active", true);
xml_parser::write_xml(std::cout, pt);
}
Resulting XML:
<?xml version="1.0" encoding="utf-8"?>
<list>
<value active="true">12</value>
<value active="true">44</value>
<value active="true">23</value>
</list>