在尝试了 networkx、lxml 和 pygraphml 之后,我决定他们根本不会做这项工作。我正在使用 BeautifulSoup 并从头开始编写所有内容:
from bs4 import BeautifulSoup
fp = "files/tes.graphml"
with open(fp) as file:
soup = BeautifulSoup(file, "lxml")
nodes = soup.findAll("node", {"yfiles.foldertype":""})
groups = soup.find_all("node", {"yfiles.foldertype":"group"})
edges = soup.findAll("edge")
然后你会得到这样的结果:
print " --- Groups --- "
for group in groups:
print group['id']
print group.find("y:nodelabel").text.strip()
print " --- Nodes --- "
for node in nodes:
print node['id']
print node.find("y:nodelabel").text.strip()
这应该让你继续前进。您可以制作组、节点和边缘对象并将它们用于某些处理。
我可能会开源我正在开发的库,因为它将用于比解析图形更大的目的。

和输出:
--- Groups ---
n0 / SimpleApp
--- Nodes ---
n0::n0 / main
n0::n1 / say hello
n1 / Exit
--- Edges ---
n0::e0 / n0::n0 / n0::n1 / str:username, int:age
e0 / n0::n1 / n1 / None