7

How would you remove a node from boost xml property tree?

I have a document like this:

<folders>
  <folder>some/folder</folder>
  <folder>some/folder</folder>
  <folder>some/folder</folder>
</folders>

I know how to itereate and print all folders, but how would i remove one of the items and save the xml back?

4

4 に答える 4

5

私はおそらく試してみます:

boost::property_tree::ptree pt;

pt.erase(key);
于 2013-04-01T14:43:25.890 に答える
2
void Backups::removeGeneric(const std::string key, const std::string value)
{
    typedef boost::property_tree::ptree Tree;

    Tree pt;
    boost::property_tree::xml_parser::read_xml(Backups::getBackupFile(), pt);

    std::pair< Tree::assoc_iterator, Tree::assoc_iterator> range = pt.equal_range(key);
    if(range.first == pt.not_found())
    {  
        std::cout << "There is nothing to remove." << std::endl;
    }
    else
    {
        bool removed = false;

        do
        {  
            if(assoc_i->second.data() == value) {
                Tree::iterator i = pt.to_iterator(assoc_i);
                pt.erase(i);
                removed = true;
                // not sure if this is completely necessary - trying
                // to guard against invalidating the iterator
                // via erase - if removed, remember to ++i!
                range = pt.equal_range(key);
                i = range.first;
            }
            else
                ++i;
         } while(i != pt.not_found());

         if(removed)
         {
             boost::property_tree::xml_parser::write_xml(Backups::getBackupFile(), pt);
             std::cout << value << " was removed." << std::endl;
         }
         else
             std::cout << value << " is not added." << std::endl;
    }
}
于 2012-10-25T04:52:24.040 に答える