How to parse GraphML files with a cool Java library

I’ve found this great GraphML reader and writer library: Blueprints. There is also a good documentation on how to use the GraphML library for reading XML-encoded graphs. Anyway, I think my example is better. 🙂

I will show you how to access the data of this XML file using the Graph Markup Language:

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="">
  <!-- Properties -->
  <key id="name" for="node""name" attr.type="string"/>
  <key id="since" for="edge""since" attr.type="int"/>
  <!-- Graph -->
  <graph id="G" edgedefault="directed">
    <!-- Vertices -->
    <node id="1">
      <data key="name">Benny</data>
    <node id="2">
      <data key="name">Daniel</data>
    <node id="3">
      <data key="name">Robert</data>
    <node id="4">
      <data key="name">Jan</data>
    <node id="5">
      <data key="name">Melanie</data>
    <node id="6">
      <data key="name">Julia</data>
    <node id="7">
      <data key="name">Josefine</data>
    <node id="8">
      <data key="name">Kristin</data>
    <!-- Edges -->
    <edge id="9" source="1" target="5" label="knows">
      <data key="since">2008</data>
    <edge id="10" source="2" target="6" label="knows" >
      <data key="since">2011</data>
    <edge id="11" source="3" target="7" label="knows">
      <data key="since">2010</data>
    <edge id="12" source="4" target="8" label="knows">
      <data key="since">2009</data>
    <edge id="13" source="2" target="5" label="knows">
      <data key="since">2011</data>
    <edge id="14" source="8" target="1" label="knows">
      <data key="since">2010</data>

Java code:

import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import java.util.Iterator;
public class App {
  private static final String XML_FILE = "./res/graph.xml";
  public static void main(String[] args) throws Exception {
    Graph graph = new TinkerGraph();
    GraphMLReader reader = new GraphMLReader(graph);
    InputStream is = new BufferedInputStream(new FileInputStream(XML_FILE));
    Iterable<Vertex> vertices = graph.getVertices();
    Iterator<Vertex> verticesIterator = vertices.iterator();
    while (verticesIterator.hasNext()) {
      Vertex vertex =;
      Iterable<Edge> edges = vertex.getInEdges();
      Iterator<Edge> edgesIterator = edges.iterator();
      while (edgesIterator.hasNext()) {
        Edge edge =;
        Vertex outVertex = edge.getOutVertex();
        Vertex inVertex = edge.getInVertex();
        String person = (String) outVertex.getProperty("name");
        String knownPerson = (String) inVertex.getProperty("name");
        int since = (Integer) edge.getProperty("since");
        String sentence = person + " " + edge.getLabel() + " " + knownPerson
                + " since " + since + ".";


Kristin knows Benny since 2010.
Robert knows Josefine since 2010.
Daniel knows Julia since 2011.
Benny knows Melanie since 2008.
Daniel knows Melanie since 2011.
Jan knows Kristin since 2009.

Ein Gedanke zu „How to parse GraphML files with a cool Java library“

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.