GraphML With JUNG: Saving Out

This one of two posts I’m dedicating to saving out to and loading from GraphML using the JUNG library. These are two parts of a really good library that lack sufficient documentation.

GraphMLWriter<MyVertex, MyEdge> graphWriter =
                 new GraphMLWriter<MyVertex, MyEdge> ();

PrintWriter out = new PrintWriter(
                      new BufferedWriter(
                          new FileWriter(filename)));

You first need to create your writers. The first line of code creates your GraphMLWriter with your custom Edge and Vertex classes specified.

The next line creates a standard Java PrintWriter, which takes in input from a BufferedWriter, which in turn is connected to a FileWriter that writes to the file specified by the String filename. (The BufferedWriter isn’t necessary in all cases)

graphWriter.addVertexData("x", null, "0",
    new Transformer<MyVertex, String>() {
        public String transform(MyVertex v) {
            return Double.toString(graphy.layout.getX(v));

graphWriter.addVertexData("y", null, "0",
    new Transformer<MyVertex, String>() {
        public String transform(MyVertex v) {
            return Double.toString(graphy.layout.getY(v));

By default, the XML file generated by the does not store the X and Y coordinates of the vertices, so when you load a graph from a file, all the vertices are placed on top of each other at one point on the canvas.

These are two bits of code that tell my GraphMLWriter to add this information in to the file it creates. The addVertexData library function looks like this:

public void addVertexData (
    String id, String description,
        String default_value,
            Transformer<V, String> vertex_transformer)

The id is the string that I want added into my XML file, and will be what I will be using to extract the value while loading my graph from the file. The description would describe the new data field you’re adding, I’ve omitted that here. The default_value is again a string, and the last argument is what makes it all work.

A Transformer can be looked as just something that transforms one object into another. It has an abstract function transform that every transformer is required to implement. In this case, the transformer is required to return a String that is needed by the GraphMLWriter, and I’ve used my AbstractLayout graphy.layout to extract the X and Y coordinates of the vertex and return them as strings to my GraphMLWriter., out);

This final bit of code saves out the Graph g using the PrintWriter out.

Note: You’ll either need to enclose the entire block of code to save your graph in a try-catch block that catches an IOException, or declare the calling function with throws IOException.

7 responses to “GraphML With JUNG: Saving Out

  1. Hi, thank you for your tuto.
    but please is it possible to tell us how to add the and attr.type of the data schema description ?

    thank you

    • Johan Joubert

      Walid, have you been able to sort this? I’m also having a problem reading in a graph into Gephi, as it also requires the and attr.type

      • Hi, where exactly do you need and attr.type? Are these a property of the vertex? There are 3 methods to add metadata to a graph:

      • addVertexData: This adds data to each vertex of the graph. The data is derived by the vertex-to-string transformer that you provide, and an example of this is in the post above.
      • addEdgeData: This is the equivalent of addVertexData for an edge.
      • addGraphData: This specifies metadata for the graph itself
      • You can find more useful methods in the GraphMLWriter JavaDocs.