Hello,
I am using jgrapht for my research since a few months and came across
the export function. As newer versions of MS Visio do not support csv
file format and - more important - open formats are to be prefered, I
tried to export a graph with the built-in GraphMLExporter.
Unfortunately there seems no vertex names or edge weights to be exported
(although I am sure, I am making a mistake here), because when I open
the exported .graphml file with yEd or Gephi there are no values of
vertexes and edges.
I already read that there are NameProvider-classes but I could not
figure out how to use them or even if they are the way to go here.
So, I would like to export a graph using GraphML where the vertexes have
names like created in the programm ("A", "B", "C") and the edges have
weights like set in the programm (1, 2, 3).
Here is a very small example class and I hope anybody could help me out
on this issue.
Thank you for reading so far!
Here is the programm code:
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.TransformerConfigurationException;
import org.jgrapht.ext.GraphMLExporter;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
import org.xml.sax.SAXException;
public class WIC {
public SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g;
public WIC(){
g = new
SimpleDirectedWeightedGraph<String,DefaultWeightedEdge>(DefaultWeightedEdge.class);
}
private void test(){
g.addVertex("A");
g.addVertex("B");
g.addVertex("C");
makeEdge("A","B",1);
makeEdge("B","A",2);
makeEdge("A","C",3);
}
private void makeEdge(String A, String B, int w){
DefaultWeightedEdge e = g.addEdge(A,B);
g.setEdgeWeight(e, w);
}
public static void main(String[] args){
WIC wic = new WIC();
wic.test();
GraphMLExporter VE = new GraphMLExporter();
FileWriter PS;
try {
PS = new FileWriter("C:/TestData/graph.graphml");
VE.export(PS, wic.g);
} catch (IOException | TransformerConfigurationException |
SAXException e) {e.printStackTrace();}
}
}
|