Boost property tree: Remove a node
Asked Answered
M

4

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?

Manteltree answered 15/6, 2012 at 9:24 Comment(0)
H
6

I would probably try:

boost::property_tree::ptree pt;

pt.erase(key);
Hydrous answered 1/4, 2013 at 14:43 Comment(1)
This will delete the first key, not a specific key .-- this is a problem if your child tree contains more than one key with the same name.Prolific
T
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;
    }
}
Truehearted answered 25/10, 2012 at 4:52 Comment(0)
M
0

Well, I did it this way:

void Backups::removeGeneric(const std::string key, const std::string value)
{
    boost::property_tree::ptree pt;
    boost::property_tree::xml_parser::read_xml(Backups::getBackupFile(), pt);

    bool remove = true;

    try {
        pt.get_child(key);
    }
    catch(boost::exception &ex)
    {  
        std::cout << "There is nothing to remove." << std::endl;
        remove = false;
    }

    if(remove)
    {  
        bool exists = false;

        boost::property_tree::ptree newPt;

        BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child(key))
        {  
            if(v.second.data() != value)
                newPt.add("scheme", v.second.data());

            if(v.second.data() == value)
                exists = true;
        }

        if(exists)
        {  
            pt.put_child(key, newPt);
            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;
    }
}
Manteltree answered 15/6, 2012 at 10:59 Comment(1)
Don't use exceptions for flow control.Anzio
T
0
   // Recursively erase nodes
   void prune_nodes(ptree& Prop_tree, const char* Node_name)
   {
        ptree::iterator Root = Prop_tree.begin(); // Node iterator
        ptree::iterator End = Prop_tree.end(); // Stop sentinel
        // Loop through current 'Root-Level'
        for(Root; Root != End; Root++) 
        {
            Root->second.erase(Node_name); // Erase nodes in current 'Root-Level' sub-tree
            prune_nodes(Root->second, Node_name); // Recurse using current sub-tree as next call's ptree
        }
   }
Tapping answered 3/10, 2017 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.