How do I write a JavaBean to an XML file using JOX?
Author: Deron Eriksson
Description: This Java tutorial describes how to write a JavaBean to an XML file using JOX (Java Objects in XML).
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

Various methods exist for writing JavaBeansW to XMLW files. A simple, elegant solution is JOX (Java Objects in XML), which can be found at http://www.wutka.com/jox.html. This solution works great for beans consisting of fairly simple data structures. Primitive data types (int, long, etc), object versions of those types (Integer, Long, etc), and Strings and Dates work great, as well as arrays of these objects. You can also have objects nested within other objects.

Your bean can contain collections like Vectors and Hashmaps within it, but these must be exposed to the outer world via getter and setter methods as arrays of the objects that make up the collections rather than as the collections themselves.

The author of the JOX library notes that JOX has its limitations as the complexity of your data increases. However, for relatively simple data, it's excellent.

This tutorial will utilize the jox116.jar and dtdparser121.jar libraries, as shown below:

'testing' project

The JoxWriteJavaBeanToFile class contains our main method. It instantiates a TestBean object and sets the testBoolean and testString fields. It then creates an array of 2 TestBean2 objects and populates 2 fields in each TestBean2 object. The TestBean2 array is then set on the TestBean object. Following this, the TestBean object is written to the jox-test.xml file. Note: the JoxReadJavaBeanFromFile class is used in another tutorial, covering how to read data from an XML file into a bean.

JoxWriteJavaBeanToFile.java

package test;

import java.io.FileOutputStream;

import com.wutka.jox.JOXBeanOutputStream;

public class JoxWriteJavaBeanToFile {

	public static void main(String[] args) {

		try {
			TestBean testBean = new TestBean();
			testBean.setTestBoolean(true);
			testBean.setTestString("test bean says hi");

			TestBean2[] testBean2Array = new TestBean2[2];

			TestBean2 testBean2A = new TestBean2();
			testBean2A.setTestBean2String1("alpha");
			testBean2A.setTestBean2String2("beta");

			TestBean2 testBean2B = new TestBean2();
			testBean2B.setTestBean2String1("gamma");
			testBean2B.setTestBean2String2("delta");

			testBean2Array[0] = testBean2A;
			testBean2Array[1] = testBean2B;

			testBean.setTestBean2Array(testBean2Array);

			FileOutputStream fileOut = new FileOutputStream("jox-test.xml");
			JOXBeanOutputStream joxOut = new JOXBeanOutputStream(fileOut);
			joxOut.writeObject("JoxTest", testBean);
			joxOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

TestBean has a boolean field, a String field, and a Vector field. As noted in the source code, the automatically generated getter/setter methods for the Vector have been commented out, and these have been replaced by methods that convert the Vector of TestBean2's to a String array of TestBean2's.

TestBean.java

package test;

import java.io.Serializable;
import java.util.Vector;

public class TestBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private boolean testBoolean;
	private String testString;
	private Vector<TestBean2> testVector;

	public TestBean() {
	}

	// Don't use getTestVector... use getTestBean2Array instead
	// public Vector getTestVector() {
	// return testVector;
	// }
	public TestBean2[] getTestBean2Array() {
		TestBean2[] testBean2Array = new TestBean2[testVector.size()];
		testVector.copyInto(testBean2Array);
		return testBean2Array;
	}

	// Don't use setTestVector... use setTestBean2Array instead
	// public void setTestVector(Vector testVector) {
	// this.testVector = testVector;
	// }
	public void setTestBean2Array(TestBean2[] testBean2Array) {
		testVector = new Vector<TestBean2>();
		for (int i = 0; i < testBean2Array.length; i++) {
			testVector.add(testBean2Array[i]);
		}
	}

	public boolean isTestBoolean() {
		return testBoolean;
	}

	public void setTestBoolean(boolean testBoolean) {
		this.testBoolean = testBoolean;
	}

	public String getTestString() {
		return testString;
	}

	public void setTestString(String testString) {
		this.testString = testString;
	}

}

TestBean2 is a simple JavaBeanW that has 2 String fields.

TestBean2.java

package test;

import java.io.Serializable;

public class TestBean2 implements Serializable {

	private static final long serialVersionUID = 1L;
	private String testBean2String1;
	private String testBean2String2;

	public TestBean2() {
	}

	public String getTestBean2String1() {
		return testBean2String1;
	}

	public void setTestBean2String1(String testBean2String1) {
		this.testBean2String1 = testBean2String1;
	}

	public String getTestBean2String2() {
		return testBean2String2;
	}

	public void setTestBean2String2(String testBean2String2) {
		this.testBean2String2 = testBean2String2;
	}

}

(Continued on page 2)

Page:    1 2 >