Advanced Topics

yFiles XSLT Support for GraphML

The yFiles GraphML extension package provides advanced conversion services for arbitrary XML-based data. By means of a suitable XSLT stylesheet any valid XML file can be transformed into a graph structure encoded in GraphML file format.

The tutorial demo application XMLXSLDemo.java shows how to import XML files as GraphML. Predefined XSLT stylesheets are available for the following XML-based file types and information:

Note that the predefined XSLT stylesheets make use of the yFiles post-processors functionality: obviously, none of the aforementioned types of XML data files provide coordinates or even dimensional information for its content. The XSLT stylesheets embed post-processors in each generated GraphML file which automatically calculate a layout when the encoded graph is loaded.

Reading and Writing Graphs of Arbitrary Type

The most convenient way to use the yFiles GraphML extension package for reading and writing GraphML file format is by using the services of class GraphMLIOHandler. However, being a true yFiles IOHandler, this class requires a Graph2D object when reading or writing a file. Using the low-level functionality of the yFiles GraphML extension package directly, it is also possible to read and write graphs of arbitrary type.

Classes DOMGraphMLParser and DirectGraphMLWriter provide the low-level services to read and write GraphML format. These classes make use of so-called graph element factories and graph element providers, respectively, which are responsible for providing objects of proper type for both the graph that is to be read or written and also its nodes and edges.

Implementations of graph element factory and graph element provider that are predefined with the yFiles GraphML extension package are classes YGraphElementFactory and YGraphElementProvider. These can be instantiated using an arbitrary graph type, and are then registered with DOMGraphMLParser and DirectGraphMLWriter, respectively.

Instantiating both graph element factory and graph element provider with class Graph, for example, enables reading and writing graphs of this type instead of Graph2D. As a result, the GraphML file format can be used without classes that are only present in the yFiles Viewer distribution.

Example 10.13, “Writing graphs of type y.base.Graph to a GraphML file” shows the configuration of class DirectGraphMLWriter so that it handles graphs of type Graph when writing a GraphML file.

Example 10.13. Writing graphs of type y.base.Graph to a GraphML file

public void encodeGraphAsGraphML(Graph graph, OutputStream out)
{
  // Using class y.base.Graph instead of y.view.Graph2D to write GraphML file 
  // format. 
  YGraphElementProvider gep = new YGraphElementProvider(graph);
  
  // Low-level support for writing GraphML file format. 
  DirectGraphMLWriter writer = new DirectGraphMLWriter();
  writer.setGraphElementProvider(gep);
  try {
    writer.write(new DomXmlWriter(out));
  }
  catch (IOException ioEx) {
    // Something went wrong. Complain. 
  }
}

Example 10.14, “Populating graphs of type y.base.Graph with data read from a GraphML file” shows the configuration of class DOMGraphMLParser so that it uses graphs of type Graph when reading a GraphML file.

Example 10.14. Populating graphs of type y.base.Graph with data read from a GraphML file

public void populateGraphFromGraphML(Graph graph, InputStream in)
{
  // Using class y.base.Graph instead of y.view.Graph2D when reading GraphML 
  // file format. 
  YGraphElementFactory gef = new YGraphElementFactory(graph);
  
  // Low-level support for processing the GraphML file format's DOM structure. 
  DOMGraphMLParser parser = new DOMGraphMLParser();
  parser.setGraphElementFactory(gef);
  try {
    parser.parse(in);
  }
  catch (IOException ioEx) {
    // Something went wrong. Complain. 
  }
}

The tutorial demo application BaseGraphSerializationDemo.java is a simple command line program that demonstrates how the yFiles GraphML extension package can be used to read and write an instance of type Graph.