【问题标题】:Boost property tree: Remove a nodeBoost 属性树:删除一个节点
【发布时间】:2012-06-15 09:24:31
【问题描述】:

如何从 boost xml 属性树中删除一个节点?

我有这样的文件:

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

我知道如何迭代和打印所有文件夹,但我将如何删除其中一项并将 xml 保存回来?

【问题讨论】:

    标签: c++ boost boost-propertytree


    【解决方案1】:

    我可能会尝试:

    boost::property_tree::ptree pt;
    
    pt.erase(key);
    

    【讨论】:

    • 这将删除第一个键,而不是特定键。--如果您的子树包含多个同名键,则会出现问题。
    【解决方案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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      嗯,我是这样做的:

      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;
          }
      }
      

      【讨论】:

      • 不要使用异常进行流量控制。
      【解决方案4】:
         // 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
              }
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 2013-01-24
        • 1970-01-01
        • 2016-10-11
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多