boost ptree-how to modify XML using iterators?
I am working on a XML file that looks like this:
<persons>
<person>
<name>NAME1</name>
<ID>ID1</ID>
</person>
<person开发者_运维百科>
<name>NAME2</name>
<ID>ID2</ID>
</person>
<person>
<name>NAME3</name>
<ID>ID3</ID>
</person>
... etc
</persons>
I need to replace all the names by "OTHERNAME". When I use the code that folows, one of the name is replaced by OTHERNAME.
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
ptree pt;
read_xml(filename,pt);
ptree &pt_persons = pt.get_child("persons");
ptree &pt_person = pt_person.get_child("person");
pt_person.put("NAME","OTHERNAME");
I, however, want to replace all the names so I tried this code:
ptree pt
read_xml(filename,pt);
ptree &pt_persons = pt.get_child("persons");
for( ptree::iterator &it = pt_persons.begin(); it != pt_persons.end();it++){
ptree &pt_person = it->second;
pt_person.put("NAME","OTHERNAME");
}
write_xml(filename, pt);
My problem is that this code doesn't do anything. My guess is that the iterator is not a reference to the node I want to modify but to a copy of this node.
Do you have any suggestion?
Thank you in advance.
In order to solve my problem I decided to use TinyXML. It is a lot easier to use for updating and modifying XML files.
精彩评论