public static void writeXMLBIF03(String filename,String graphName,ArrayList<GraphNode> nodes,ArrayList<GraphEdge> edges){
  try {
    FileWriter outfile=new FileWriter(filename);
    StringBuffer text=new StringBuffer();
    text.append("<?xml version=\"1.0\"?>\n");
    text.append("<!-- DTD for the XMLBIF 0.3 format -->\n");
    text.append("<!DOCTYPE BIF [\n");
    text.append("	<!ELEMENT BIF ( NETWORK )*>\n");
    text.append("	      <!ATTLIST BIF VERSION CDATA #REQUIRED>\n");
    text.append("	<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFI" + "NITION )* )>\n");
    text.append("	<!ELEMENT NAME (#PCDATA)>\n");
    text.append("	<!ELEMENT VARIABLE ( NAME, ( OUTCOME |  PROPERTY )* )" + " >\n");
    text.append("	      <!ATTLIST VARIABLE TYPE (nature|decision|utility" + ") \"nature\">\n");
    text.append("	<!ELEMENT OUTCOME (#PCDATA)>\n");
    text.append("	<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY" + " )* >\n");
    text.append("	<!ELEMENT FOR (#PCDATA)>\n");
    text.append("	<!ELEMENT GIVEN (#PCDATA)>\n");
    text.append("	<!ELEMENT TABLE (#PCDATA)>\n");
    text.append("	<!ELEMENT PROPERTY (#PCDATA)>\n");
    text.append("]>\n");
    text.append("\n");
    text.append("\n");
    text.append("<BIF VERSION=\"0.3\">\n");
    text.append("<NETWORK>\n");
    text.append("<NAME>" + XMLNormalize(graphName) + "</NAME>\n");
    for (int nodeidx=0; nodeidx < nodes.size(); nodeidx++) {
      GraphNode n=nodes.get(nodeidx);
      if (n.nodeType != GraphNode.NORMAL) {
        continue;
      }
      text.append("<VARIABLE TYPE=\"nature\">\n");
      text.append("\t<NAME>" + XMLNormalize(n.ID) + "</NAME>\n");
      if (n.outcomes != null) {
        for (        String outcome : n.outcomes) {
          text.append("\t<OUTCOME>" + XMLNormalize(outcome) + "</OUTCOME>\n");
        }
      }
 else {
        text.append("\t<OUTCOME>true</OUTCOME>\n");
      }
      text.append("\t<PROPERTY>position = (" + n.x + ","+ n.y+ ")</PROPERTY>\n");
      text.append("</VARIABLE>\n");
    }
    for (int nodeidx=0; nodeidx < nodes.size(); nodeidx++) {
      GraphNode n=nodes.get(nodeidx);
      if (n.nodeType != GraphNode.NORMAL) {
        continue;
      }
      text.append("<DEFINITION>\n");
      text.append("<FOR>" + XMLNormalize(n.ID) + "</FOR>\n");
      int parntOutcomes=1;
      if (n.prnts != null) {
        for (        int prnt2 : n.prnts) {
          GraphNode prnt=nodes.get(prnt2);
          text.append("\t<GIVEN>" + XMLNormalize(prnt.ID) + "</GIVEN>\n");
          if (prnt.outcomes != null) {
            parntOutcomes*=prnt.outcomes.length;
          }
        }
      }
      text.append("<TABLE>\n");
      for (int i=0; i < parntOutcomes; i++) {
        if (n.outcomes != null) {
          for (int outidx=0; outidx < n.outcomes.length; outidx++) {
            text.append(n.probs[i][outidx] + " ");
          }
        }
 else {
          text.append("1");
        }
        text.append('\n');
      }
      text.append("</TABLE>\n");
      text.append("</DEFINITION>\n");
    }
    text.append("</NETWORK>\n");
    text.append("</BIF>\n");
    outfile.write(text.toString());
    outfile.close();
  }
 catch (  IOException ex) {
    ex.printStackTrace();
  }
}

public static void writeXMLBIF03 ( String filename , String graphName , ArrayList < GraphNode > nodes , ArrayList < GraphEdge > edges ) { try { FileWriter outfile = new FileWriter ( filename ) ; StringBuffer text = new StringBuffer ( ) ; text . append ( "<?xml version=\"1.0\"?>\n" ) ; text . append ( "<!-- DTD for the XMLBIF 0.3 format -->\n" ) ; text . append ( "<!DOCTYPE BIF [\n" ) ; text . append ( "	<!ELEMENT BIF ( NETWORK )*>\n" ) ; text . append ( "	      <!ATTLIST BIF VERSION CDATA #REQUIRED>\n" ) ; text . append ( "	<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFI" + "NITION )* )>\n" ) ; text . append ( "	<!ELEMENT NAME (#PCDATA)>\n" ) ; text . append ( "	<!ELEMENT VARIABLE ( NAME, ( OUTCOME |  PROPERTY )* )" + " >\n" ) ; text . append ( "	      <!ATTLIST VARIABLE TYPE (nature|decision|utility" + ") \"nature\">\n" ) ; text . append ( "	<!ELEMENT OUTCOME (#PCDATA)>\n" ) ; text . append ( "	<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY" + " )* >\n" ) ; text . append ( "	<!ELEMENT FOR (#PCDATA)>\n" ) ; text . append ( "	<!ELEMENT GIVEN (#PCDATA)>\n" ) ; text . append ( "	<!ELEMENT TABLE (#PCDATA)>\n" ) ; text . append ( "	<!ELEMENT PROPERTY (#PCDATA)>\n" ) ; text . append ( "]>\n" ) ; text . append ( "\n" ) ; text . append ( "\n" ) ; text . append ( "<BIF VERSION=\"0.3\">\n" ) ; text . append ( "<NETWORK>\n" ) ; text . append ( "<NAME>" + XMLNormalize ( graphName ) + "</NAME>\n" ) ; for ( int nodeidx = 0 ; nodeidx < nodes . size ( ) ; nodeidx ++ ) { GraphNode n = nodes . get ( nodeidx ) ; if ( n . nodeType != GraphNode . NORMAL ) { continue ; } text . append ( "<VARIABLE TYPE=\"nature\">\n" ) ; text . append ( "\t<NAME>" + XMLNormalize ( n . ID ) + "</NAME>\n" ) ; if ( n . outcomes != null ) { for ( String outcome : n . outcomes ) { text . append ( "\t<OUTCOME>" + XMLNormalize ( outcome ) + "</OUTCOME>\n" ) ; } } else { text . append ( "\t<OUTCOME>true</OUTCOME>\n" ) ; } text . append ( "\t<PROPERTY>position = (" + n . x + "," + n . y + ")</PROPERTY>\n" ) ; text . append ( "</VARIABLE>\n" ) ; } for ( int nodeidx = 0 ; nodeidx < nodes . size ( ) ; nodeidx ++ ) { GraphNode n = nodes . get ( nodeidx ) ; if ( n . nodeType != GraphNode . NORMAL ) { continue ; } text . append ( "<DEFINITION>\n" ) ; text . append ( "<FOR>" + XMLNormalize ( n . ID ) + "</FOR>\n" ) ; int parntOutcomes = 1 ; if ( n . prnts != null ) { for ( int prnt2 : n . prnts ) { GraphNode prnt = nodes . get ( prnt2 ) ; text . append ( "\t<GIVEN>" + XMLNormalize ( prnt . ID ) + "</GIVEN>\n" ) ; if ( prnt . outcomes != null ) { parntOutcomes *= prnt . outcomes . length ; } } } text . append ( "<TABLE>\n" ) ; for ( int i = 0 ; i < parntOutcomes ; i ++ ) { if ( n . outcomes != null ) { for ( int outidx = 0 ; outidx < n . outcomes . length ; outidx ++ ) { text . append ( n . probs [ i ] [ outidx ] + " " ) ; } } else { text . append ( "1" ) ; } text . append ( '\n' ) ; } text . append ( "</TABLE>\n" ) ; text . append ( "</DEFINITION>\n" ) ; } text . append ( "</NETWORK>\n" ) ; text . append ( "</BIF>\n" ) ; outfile . write ( text . toString ( ) ) ; outfile . close ( ) ; } catch ( IOException ex ) { ex . printStackTrace ( ) ; } }
this method writes a graph in xmlbif ver . 0 . 3 format to a file . however , if is reloaded in graphvisualizer we would need to layout the graph again to display it correctly .
