我有以下 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>
我想用 and 删除边缘的start
and属性。end
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
.