This tip will show the way to read the xml file and extract the bean from it using XMLDecoder.

In this example object of the java bean MyBean will be extracted from 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 extracts a java object from the XML: BeanToXMLExp.java

Java Code:
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class XMLToBeanExp {
	public static void main(String[] args) {
		try {
			XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
					new FileInputStream("C:\\MyXML.xml")));
			MyBean myBean = (MyBean) decoder.readObject();
			decoder.close();
			System.out.println(myBean.getAge());
			System.out.println(myBean.getName());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}