简体   繁体   中英

Processing yEd graphml file in python

I want to get a list of all nodes and some attributes (eg label name) in a yEd created graphml file regardless of where they are located in the graph. This has been partially dealt with already ( Processing XML file with networkx in python and How to iterate over GraphML file with lxml ) but not when you 'group' nodes within yEd - and I have lots of groupings within groupings.

Have tried networkx and lxml but not getting complete set of results using simple approaches suggested - any suggestions on elegant way to resolve and which library to use short of recursively iterating through tree and identifying group nodes and drilling down again.

Example:

Sample output for very simple graph using networkx when you have groupings:

('n0', {})
('n1', {'y': '0.0', 'x': '26.007967509920633', 'label': 'A'})
('n0::n0', {})
('n0::n1', {})

图形的简单表示

I think you can try this out.

It is a Python library that, as per the author...

provides an easy interface that lets you specify how a graph should look, and generates corresponding graphML that can be opened in yEd.

https://github.com/jamesscottbrown/pyyed

Hope this helps!

Cheers!

After trying out networkx, lxml and pygraphml, I decided they won't do the job at all. I'm using BeautifulSoup and writing everything from the ground up:

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")

Then you get your results like this:

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()

This should get you going. You can make Group, Node & Edge objects and use them for some processing.

I may open source the library I am working on as it would be used for a bigger purpose than just parsing graphs.

在此处输入图片说明

And the output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM