Sample instances from a BIF XML file using WEKA

The WEKA machine learning library (version 3.7.3) offers the class BayesNetGenerator to generate Bayesian networks artificially. The class provides a method called generateInstances; to sample instances from a randomly generated network. However, for me it was not clear how to use this code to sample instances from a given Bayesian network stored in a BIF XML file.
Assume that your network file is stored at path filename and your generated instances should be saved in ARFF format to target, you can use the following code:

public void generateDataFromXmlFile(String filename, String taget) throws Exception
    // create a weka BayesNetGenerator
    BayesNetGenerator generator = new BayesNetGenerator();

    // clear stack

    // set the bif xml file

    // define the number of instances to be sampled

    // generate a "random" network
    // is internally generated with the bif xml file

    // generates the instances

    // write the instances to a ARFF file
    StringBuffer text = new StringBuffer();
    FileWriter outfile = new FileWriter(taget);

First, the BayesNetGenerator object is created and the stack is cleared (not sure if this is needed?).
A BIF XML file can be set as template to the generator by calling


where filename is the path to the BIF XML file. After setting the number of samples to be created to 1000, the generator is asked to create a random network. Note that the network is NOT generated randomly, but read form the BIF XML file as we defined the file before.
Lastly, the instances are written to a ARFF file and we’re done :-)