Boost属性树:从节点中删除属性

时间:2013-02-08 20:18:24

标签: c++ c++11 boost boost-propertytree

我有以下XML文件:

<?xml version="1.0" encoding="utf-8"?>
<gexf>
  <graph>
    <nodes>
      <node id="0" label="0" start="0" end="25"/>
      <node id="1" label="1" start="1"/>
      <node id="2" label="2" start="2"/>
      ...
    </nodes>
    <edges>
      <edge id="0" source="0" target="1" start="7" end="19"/>
      <edge id="1" source="0" target="2" start="8" end="20"/>
      ...
    </edges>
  </graph>
</gexf>

我想要使用startend从边缘移除source="0"target="1"属性。

我试图这样做的方法如下。假设XML文件名为ptree_test.gexf,我将其读入,在树中找到正确的边,然后尝试使用erase来删除属性。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

using boost::property_tree::ptree;

int main(int argc, char *argv[]) {

  ptree pt;

  read_xml("ptree_test.gexf", pt);

  // Now find edge (0, 1) and delete the start and end attributes
  ptree edge;
  int id1, id2;
  id1 = 0;
  id2 = 1;

  for(auto &e : pt.get_child("gexf.graph.edges")) {
    int s, t;
    s = e.second.get<int>("<xmlattr>.source");
    t = e.second.get<int>("<xmlattr>.target");

    // Check if this is the correct edge
    // Two checks because it can be reversed
    if((id1 == s && id2 == t) || (id1 == t && id2 == s)) {
      edge = e.second;
      break;
    }
  }

  for(auto & attr : edge.get_child("<xmlattr>")) {
    if(attr.first == "end" || attr.first == "start") {
      edge.erase(attr.first);
    }
  }

  write_xml(std::cout, pt);
  return 0;
}

这不起作用。它不会删除该属性。实际上,如果我输入一个打印edge.erase(attr.first)返回值的调试语句,它会显示0

2 个答案:

答案 0 :(得分:0)

在回答之前,我想再次劝阻您不要将Boost.PropertyTree用作快速而肮脏的XML处理系统。请use a real XML parser;有很多可供选择,有些非常有效,几乎不需要依赖维护。

无论如何,您的问题来自your use of erase。您正在尝试从迭代的列表中删除元素。那不行。并非没有特殊的循环编码。

所以你不能在这里使用基于范围的for循环。你必须在迭代器上使用真正的for循环。

auto &children = edge.get_child();
for(auto &attrIt = children.begin(); attrIt != children.end();)
{
  auto &attr = *attrIt;
  //Do stuff here.


  if(attr.first == "end" || attr.first == "start")
    attrIt = children.erase(attrIt);
  else
    ++attrIt;
}

答案 1 :(得分:0)

主要问题是您在此行中制作了子树的副本:

  edge = e.second;

然后修改副本而不是原始文件。后来,正如@NicolBolas所说,你需要一个erase的交互者。完整代码如下所示:

int main(){
        boost::property_tree::ptree pt;
        read_xml("ptree_test.gexf", pt, boost::property_tree::xml_parser::trim_whitespace);
        int id1, id2;
        id1 = 0;
        id2 = 1;
        for(auto &e : pt.get_child("gexf.graph.edges")) {
            int s, t;
            s = e.second.get<int>("<xmlattr>.source");
            t = e.second.get<int>("<xmlattr>.target");
            // Check if this is the correct edge
            // Two checks because it can be reversed
            if((id1 == s && id2 == t) || (id1 == t && id2 == s)){
                auto &children = e.second.get_child("<xmlattr>");
                for(auto attrIt = children.begin(); attrIt != children.end(); ++attrIt){
                  if(attrIt->first == "end" || attrIt->first == "start")
                    attrIt = children.erase(attrIt);
                }
                break; // maybe shouldn't be here to keep looking?
            }
        }
        write_xml("ptree_test_copy.gexf", pt, std::locale(), bpt::xml_writer_settings<std::string>{'\t', 1});
}

beforeandafter