Class: XmlTutorial
This tutorial illustrates how to use the XML file format parser and writer to save and load graphs. In this example, the parser is used to read a graph from an input file, display it, and then write it to an output file.
The code to parser the file is as simple as:
try { parser.parse(args[0]); } catch(Exception e) { System.exit(-1); }
Where the argument is a URI and exceptions might be thrown if there is any problem parsing the file (it cannot be found, it is badly formatted, etc.).
Once the file has been parsed, accessing the graph model and add it to the display:
JGraph g = new JGraph(); [...] GraphModel model = parser.getParsedGraphModel(); g.getGraphPane().setGraphModel(model);
Finally, write the file back to the output file:
try { DataOutputStream out = new DataOutputStream(new FileOutputStream(argv[1])); new GraphWriter().write(model, out); } catch(Exception e) { System.exit(-1); }