Using boost::property_tree
Dear all,
Having the following xml file:
<A>
<B/>
<B attr1="value1" attr2="value2" />
<C>
<D />
<E attr1="value3" />
<E attr1="value4" />
<F />
</C>
</A>
Trying boost::property_tree I want to:
1. delete first node <E attr1="value3" />
2. update the value of attr1 in the second node <E attr1="value4" />
However, it failed to compile.
Could you please help me.
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/foreach.hpp>
#include <iostream>
using namespace boost::property_tree;
void create( const std::string & inputFileName, const std::string & outputFileName )
{
ptree pt;
read_xml(inputFileName, pt);
ptree & Alphas= pt.get_child("A.C");
ptree::iterator it=Alphas.begin();
it++;
it++;
Alphas.erase(it);
BOOST_FOREACH(ptree::value_type &v, pt.get_child("A.C")){
if (v.first=="E"){
v.second.set_value("<xmlattr>.attr1","new_value4");
}
}
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml(outputFileName, pt, std::locale(), settings);
}
int main(){
create("example_t.xml","new_example.xml");
return 0;
}
| |
The tree does not have a function set_value(...). It is either put_value(...) or in this case put(...).
Topic archived. No new replies allowed.