ファイル入出力

XML 形式で入出力します。

Graph のファイル出力

Graph g=GraphLib.getGrid(3,3);
 
    GraphMLWriter gw=new GraphMLWriter();
    FileOutputStream fos;
 
    try {
      fos=new FileOutputStream("graph.xml");
      gw.writeGraph(g,fos);
      fos.close();
    } catch (IOException e){
      e.printStackTrace();
      System.exit(1);
    } catch (DataIOException e){
      e.printStackTrace();
      System.exit(2);
    }
 

Graph のファイル入力

GraphMLReader gr=new GraphMLReader();
    FileInputStream fis;
    Graph graph=null;
 
    try {
      fis=new FileInputStream("graph.xml");
      graph=gr.readGraph(fis);
      fis.close();
    } catch (IOException e){
      e.printStackTrace();
      System.exit(3);
    } catch (DataIOException e){
      e.printStackTrace();
      System.exit(4);
    }
 
 

出来上がった XML

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
  http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
 
  <!-- prefuse GraphML Writer | Mon Feb 02 00:42:10 JST 2009 -->
  <key id="label" for="node" attr.name="label" attr.type="string">
    <default></default>
  </key>
 
  <graph edgedefault="undirected">
    <!-- nodes -->
    <node id="0">
      <data key="label">0</data>
    </node>
    <node id="1">
      <data key="label">1</data>
    </node>
    <node id="2">
      <data key="label">2</data>
    </node>
    <node id="3">
      <data key="label">3</data>
    </node>
    <node id="4">
      <data key="label">4</data>
    </node>
    <node id="5">
      <data key="label">5</data>
    </node>
    <node id="6">
      <data key="label">6</data>
    </node>
    <node id="7">
      <data key="label">7</data>
    </node>
    <node id="8">
      <data key="label">8</data>
    </node>
 
    <!-- edges -->
    <edge id="0" source="0" target="1"/>
    <edge id="1" source="1" target="2"/>
    <edge id="2" source="0" target="3"/>
    <edge id="3" source="1" target="4"/>
    <edge id="4" source="3" target="4"/>
    <edge id="5" source="2" target="5"/>
    <edge id="6" source="4" target="5"/>
    <edge id="7" source="3" target="6"/>
    <edge id="8" source="4" target="7"/>
    <edge id="9" source="6" target="7"/>
    <edge id="10" source="5" target="8"/>
    <edge id="11" source="7" target="8"/>
  </graph>
</graphml>
 
 



最終更新:2009年02月02日 00:43