Create GraphML XML file with Java

With Blueprints it is very easy to create a XML file following the GraphML standard. All you need is this:

import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.pgm.util.io.graphml.GraphMLWriter;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
public class App {
 
  private static final String OUTPUT_FILE = "./res/graph.graphml";
 
  public static void main(String[] args) throws Exception {
    OutputStream out = new FileOutputStream(OUTPUT_FILE);
    TinkerGraph graph = new TinkerGraph();
 
    Vertex source = graph.addVertex("1");
    Vertex target = graph.addVertex("2");
    graph.addEdge("3", source, target, "connection");
 
    GraphMLWriter writer = new GraphMLWriter(graph);
    writer.outputGraph(out);
  }
}

This is what you will get then:

graph.graphml

<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
  <graph id="G" edgedefault="directed">
    <node id="2"></node>
    <node id="1"></node>
    <edge id="3" source="1" target="2" label="connection"></edge>
  </graph>
</graphml>

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.