This tip will show the way to convert the java bean file to the xml file using the XMLEncoder.

In this example object of the java bean MyBean will be written to the MyXML.xml file.

Class of the Java Bean: MyBean.java

Java Code:
public class MyBean {
	private String name = null;
	private int age;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
Java class that writes a java object to the XML: BeanToXMLExp.java

Java Code:
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class BeanToXMLExp {
	public static void main(String[] args) {
		try {
			MyBean myBean = new MyBean();
			myBean.setName("Tom");
			myBean.setAge(28);
			XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
					new FileOutputStream("C:\\MyXML.xml")));
			encoder.writeObject(myBean);
			encoder.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}