In my article „Create GraphML XML file with Java“ I showed how to write a Graph to a GraphML-compatible XML file. Now I want to show you how you can write a GraphML-compatible file that can be used in the yEd Graph Editor to get visualized later.
Main Class
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) throws Exception {
OutputStream out = new FileOutputStream(new File("./test.graphml"));
TinkerGraph graph = new TinkerGraph();
Vertex source = graph.addVertex("1");
Vertex target = graph.addVertex("2");
graph.addEdge("3", source, target, "connection");
YedFileWriter writer = new YedFileWriter(graph);
writer.outputGraph(out);
}
} |
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) throws Exception {
OutputStream out = new FileOutputStream(new File("./test.graphml"));
TinkerGraph graph = new TinkerGraph();
Vertex source = graph.addVertex("1");
Vertex target = graph.addVertex("2");
graph.addEdge("3", source, target, "connection");
YedFileWriter writer = new YedFileWriter(graph);
writer.outputGraph(out);
}
}
Specific GraphML-yEd-Writer
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
/**
* YedFileWriter writes a Graph for the yEd Graph Editor to a GraphML OutputStream.
*
* @author Benny Neugebauer (https://www.bennyn.de)
*/
public class YedFileWriter {
private Graph graph = null;
private String xml = null;
public YedFileWriter(Graph graph) {
this.graph = graph;
}
private String getGraphMLHeader() {
String header = "<?xml version=\"1.0\" ?>";
header += "\r\n<graphml\r\n xmlns=\"http://graphml.graphdrawing.org/xmlns\"\r\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n xmlns:y=\"http://www.yworks.com/xml/graphml\"\r\n xmlns:yed=\"http://www.yworks.com/xml/yed/3\"\r\n xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\r\n http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd\"\r\n>";
header += "\r\n <key for=\"node\" id=\"d5\" attr.name=\"description\" attr.type=\"string\" />";
header += "\r\n <key for=\"node\" id=\"d6\" yfiles.type=\"nodegraphics\"/>";
header += "\r\n <graph id=\"G\" edgedefault=\"directed\">";
return header;
}
private String getGraphMLFooter() {
String footer = "\r\n </graph>\r\n</graphml>";
return footer;
}
private String getNode(String id) {
String node = "\r\n <node id=\"" + id + "\">";
node += "\r\n <data key=\"d5\"/>";
node += "\r\n <data key=\"d6\">";
node += "\r\n <y:ShapeNode>";
node += "\r\n <y:NodeLabel>" + id + "</y:NodeLabel>";
node += "\r\n <y:Shape type=\"rectangle\"/>";
node += "\r\n </y:ShapeNode>";
node += "\r\n </data>";
node += "\r\n </node>";
return node;
}
private String getEdge(Edge edge) {
Vertex source = edge.getOutVertex();
Vertex target = edge.getInVertex();
String edgeId = (String) edge.getId();
String sourceId = (String) source.getId();
String targetId = (String) target.getId();
String label = edge.getLabel();
String edgeXml = "\r\n <edge id=\"" + edgeId + "\" source=\"" + sourceId + "\" target=\"" + targetId + "\" label=\"" + label + "\">";
edgeXml += "\r\n </edge>";
return edgeXml;
}
private void createGraphXml() {
xml = getGraphMLHeader();
// Create nodes
Iterable<Vertex> vertices = graph.getVertices();
Iterator<Vertex> verticesIterator = vertices.iterator();
while (verticesIterator.hasNext()) {
Vertex vertex = verticesIterator.next();
String id = (String) vertex.getId();
String node = getNode(id);
xml += node;
}
// Create edges
Iterable<Edge> edges = graph.getEdges();
Iterator<Edge> edgesIterator = edges.iterator();
while (edgesIterator.hasNext()) {
Edge edge = edgesIterator.next();
String edgeXml = getEdge(edge);
xml += edgeXml;
}
xml += getGraphMLFooter();
}
public void outputGraph(final OutputStream out) throws IOException {
createGraphXml();
System.out.println(xml);
try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out))) {
br.write(xml);
br.flush();
}
out.flush();
out.close();
}
} |
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
/**
* YedFileWriter writes a Graph for the yEd Graph Editor to a GraphML OutputStream.
*
* @author Benny Neugebauer (https://www.bennyn.de)
*/
public class YedFileWriter {
private Graph graph = null;
private String xml = null;
public YedFileWriter(Graph graph) {
this.graph = graph;
}
private String getGraphMLHeader() {
String header = "<?xml version=\"1.0\" ?>";
header += "\r\n<graphml\r\n xmlns=\"http://graphml.graphdrawing.org/xmlns\"\r\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n xmlns:y=\"http://www.yworks.com/xml/graphml\"\r\n xmlns:yed=\"http://www.yworks.com/xml/yed/3\"\r\n xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\r\n http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd\"\r\n>";
header += "\r\n <key for=\"node\" id=\"d5\" attr.name=\"description\" attr.type=\"string\" />";
header += "\r\n <key for=\"node\" id=\"d6\" yfiles.type=\"nodegraphics\"/>";
header += "\r\n <graph id=\"G\" edgedefault=\"directed\">";
return header;
}
private String getGraphMLFooter() {
String footer = "\r\n </graph>\r\n</graphml>";
return footer;
}
private String getNode(String id) {
String node = "\r\n <node id=\"" + id + "\">";
node += "\r\n <data key=\"d5\"/>";
node += "\r\n <data key=\"d6\">";
node += "\r\n <y:ShapeNode>";
node += "\r\n <y:NodeLabel>" + id + "</y:NodeLabel>";
node += "\r\n <y:Shape type=\"rectangle\"/>";
node += "\r\n </y:ShapeNode>";
node += "\r\n </data>";
node += "\r\n </node>";
return node;
}
private String getEdge(Edge edge) {
Vertex source = edge.getOutVertex();
Vertex target = edge.getInVertex();
String edgeId = (String) edge.getId();
String sourceId = (String) source.getId();
String targetId = (String) target.getId();
String label = edge.getLabel();
String edgeXml = "\r\n <edge id=\"" + edgeId + "\" source=\"" + sourceId + "\" target=\"" + targetId + "\" label=\"" + label + "\">";
edgeXml += "\r\n </edge>";
return edgeXml;
}
private void createGraphXml() {
xml = getGraphMLHeader();
// Create nodes
Iterable<Vertex> vertices = graph.getVertices();
Iterator<Vertex> verticesIterator = vertices.iterator();
while (verticesIterator.hasNext()) {
Vertex vertex = verticesIterator.next();
String id = (String) vertex.getId();
String node = getNode(id);
xml += node;
}
// Create edges
Iterable<Edge> edges = graph.getEdges();
Iterator<Edge> edgesIterator = edges.iterator();
while (edgesIterator.hasNext()) {
Edge edge = edgesIterator.next();
String edgeXml = getEdge(edge);
xml += edgeXml;
}
xml += getGraphMLFooter();
}
public void outputGraph(final OutputStream out) throws IOException {
createGraphXml();
System.out.println(xml);
try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out))) {
br.write(xml);
br.flush();
}
out.flush();
out.close();
}
}