You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Boost Graph Library读取GraphML文件时解析错误求助

解决Boost Graph Library读取GraphML时的解析错误

问题原因

你遇到的parse error: unrecognized type ""错误,核心原因是写入GraphML文件时,顶点属性name的类型声明为空,导致读取时Boost无法识别属性类型。

当使用boost::vertex_name_t绑定std::string类型属性时,默认情况下write_graphml无法自动推断并写入正确的属性类型,生成的GraphML文件中对应<key>标签的attr.type属性会是空值,例如:

<key id="d0" for="node" attr.name="name" attr.type=""/>

解决方案

修改保存代码中的dynamic_properties属性注册逻辑,显式指定属性的类型,让write_graphml生成正确的类型声明:

修改后的保存代码

... variables ...

typedef boost::property<boost::vertex_name_t, std::string> Node_name;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, Node_name> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef std::unordered_map<std::string, Vertex> VertexMap;

Graph g;
VertexMap vertex_map;

... build the graph ...

std::string name = "../tmp/" + analyzed_file + ".graphml";
std::ofstream graph_ML(name);
boost::dynamic_properties dp;
// 显式指定属性类型为std::string,确保写入正确的类型声明
dp.property("name", boost::get(boost::vertex_name, g), std::string());

boost::write_graphml(graph_ML, g, dp);

验证修改效果

修改后生成的GraphML文件中,对应name属性的<key>标签会包含正确的类型声明:

<key id="d0" for="node" attr.name="name" attr.type="string"/>

此时使用你原有的读取代码即可正常解析文件,不会再抛出类型识别错误。

额外检查点

如果修改后仍有问题,可检查以下两点:

  • 确保读取代码中的Graph类型与保存时完全一致(你的代码中已满足)
  • 确认GraphML文件开头包含正确的GraphML命名空间声明:
    <?xml version="1.0" encoding="UTF-8"?>
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
    

内容的提问来源于stack exchange,提问作者StressedStudent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 13:27:54