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="http://graphml.graphdrawing.org/xmlns">
<!-- Properties -->
<key id="name" for="node" attr.name="name" attr.type="string"/>
<key id="since" for="edge" attr.name="since" attr.type="int"/>
<!-- Graph -->
<graph id="G" edgedefault="directed">
<!-- Vertices -->
<node id="1">
<data key="name">Benny</data>
</node>
<node id="2">
<data key="name">Daniel</data>
</node>
<node id="3">
<data key="name">Robert</data>
</node>
<node id="4">
<data key="name">Jan</data>
</node>
<node id="5">
<data key="name">Melanie</data>
</node>
<node id="6">
<data key="name">Julia</data>
</node>
<node id="7">
<data key="name">Josefine</data>
</node>
<node id="8">
<data key="name">Kristin</data>
</node>
<!-- Edges -->
<edge id="9" source="1" target="5" label="knows">
<data key="since">2008</data>
</edge>
<edge id="10" source="2" target="6" label="knows" >
<data key="since">2011</data>
</edge>
<edge id="11" source="3" target="7" label="knows">
<data key="since">2010</data>
</edge>
<edge id="12" source="4" target="8" label="knows">
<data key="since">2009</data>
</edge>
<edge id="13" source="2" target="5" label="knows">
<data key="since">2011</data>
</edge>
<edge id="14" source="8" target="1" label="knows">
<data key="since">2010</data>
</edge>
</graph>
</graphml> |
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
<!-- Properties -->
<key id="name" for="node" attr.name="name" attr.type="string"/>
<key id="since" for="edge" attr.name="since" attr.type="int"/>
<!-- Graph -->
<graph id="G" edgedefault="directed">
<!-- Vertices -->
<node id="1">
<data key="name">Benny</data>
</node>
<node id="2">
<data key="name">Daniel</data>
</node>
<node id="3">
<data key="name">Robert</data>
</node>
<node id="4">
<data key="name">Jan</data>
</node>
<node id="5">
<data key="name">Melanie</data>
</node>
<node id="6">
<data key="name">Julia</data>
</node>
<node id="7">
<data key="name">Josefine</data>
</node>
<node id="8">
<data key="name">Kristin</data>
</node>
<!-- Edges -->
<edge id="9" source="1" target="5" label="knows">
<data key="since">2008</data>
</edge>
<edge id="10" source="2" target="6" label="knows" >
<data key="since">2011</data>
</edge>
<edge id="11" source="3" target="7" label="knows">
<data key="since">2010</data>
</edge>
<edge id="12" source="4" target="8" label="knows">
<data key="since">2009</data>
</edge>
<edge id="13" source="2" target="5" label="knows">
<data key="since">2011</data>
</edge>
<edge id="14" source="8" target="1" label="knows">
<data key="since">2010</data>
</edge>
</graph>
</graphml>
Java code:
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.pgm.util.io.graphml.GraphMLReader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
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));
reader.inputGraph(is);
Iterable<Vertex> vertices = graph.getVertices();
Iterator<Vertex> verticesIterator = vertices.iterator();
while (verticesIterator.hasNext()) {
Vertex vertex = verticesIterator.next();
Iterable<Edge> edges = vertex.getInEdges();
Iterator<Edge> edgesIterator = edges.iterator();
while (edgesIterator.hasNext()) {
Edge edge = edgesIterator.next();
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 + ".";
System.out.println(sentence);
}
}
}
} |
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.pgm.util.io.graphml.GraphMLReader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
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));
reader.inputGraph(is);
Iterable<Vertex> vertices = graph.getVertices();
Iterator<Vertex> verticesIterator = vertices.iterator();
while (verticesIterator.hasNext()) {
Vertex vertex = verticesIterator.next();
Iterable<Edge> edges = vertex.getInEdges();
Iterator<Edge> edgesIterator = edges.iterator();
while (edgesIterator.hasNext()) {
Edge edge = edgesIterator.next();
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 + ".";
System.out.println(sentence);
}
}
}
}
Output:
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. |
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“