import java.io.StringWriter; import java.io.File; import org.apache.commons.betwixt.io.BeanWriter; import org.apache.commons.betwixt.io.BeanReader; public class BetwixtDemo{ public static void main(String args[]){ // to use Betwixt, create an instance of a BeanWriter // since a constructor for BeanWriter takes an instance of a // writer object, we will start by writing to a StringWriter StringWriter outputWriter = new StringWriter(); // note that the output is not well formed and therefore needs the // following at the top: outputWriter.write(""); // create the BeanWriter BeanWriter writer = new BeanWriter(outputWriter); // now for this writer, we can set various parameters // the first one disables writing of Id's and the second one enables // formatted output writer.setWriteIDs(false); writer.enablePrettyPrint(); // finally create a bean and write it out Mortgage mortgage = new Mortgage(6.5f, 25); // output it to standard output try{ writer.write("mortgage", mortgage); System.err.println(outputWriter.toString()); }catch(Exception e){ System.err.println(e); } // to see how Betwixt can read XML data and create beans based on it // we will use the BeanReader class. Note that this class extends // the Digester class of the Digester package. BeanReader reader = new BeanReader(); // register the class try{ reader.registerBeanClass(Mortgage.class); // and parse it Mortgage mortgageConverted = (Mortgage)reader.parse(new File("mortgage.xml")); // Let's see if this converted mortage contains the values in the file System.err.println("Values in file: Rate: " + mortgageConverted.getRate() + ", Years: " + mortgageConverted.getYears()); }catch(Exception ee){ ee.printStackTrace(); } } }