How do I read a JavaBean from an XML file using XMLDecoder?
Author: Deron Eriksson
Description: This Java tutorial shows how to read a JavaBean from an XML file using Java's XMLDecoder class.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page:    1 2 >

In another tutorial, we saw how we could use the XMLEncoder class to save a JavaBeanW object to an XMLW file. Here, we'll see how we can use the XMLDecoder class to read an XML file of data into a JavaBean object.

In the other tutorial, we wrote to a "mybean.xml" file. We'll read from this file.

mybean.xml

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.5.0_09" class="java.beans.XMLDecoder"> 
 <object class="example.MyBean"> 
  <void property="myBoolean"> 
   <boolean>true</boolean> 
  </void> 
  <void property="myString"> 
   <string>xml is cool</string> 
  </void> 
  <void property="myVector"> 
   <object class="java.util.Vector"> 
    <void method="add"> 
     <string>one</string> 
    </void> 
    <void method="add"> 
     <string>two</string> 
    </void> 
    <void method="add"> 
     <string>three</string> 
    </void> 
   </object> 
  </void> 
 </object> 
</java> 

The demonstration project's structure is shown here. The "mybean.xml" file is located at the top level of the project. The XmlEncodeToFile.java file was created in the other tutorial.

mybean.xml located at top level of project

The XmlDecodeFromFile class reads from the "mybean.xml" file as an input stream. The readObject() method of XMLDecoder is used to read a MyBean object from the file. After obtaining the MyBean object, we display the values of its fields.

XmlDecodeFromFile.java

package example;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class XmlDecodeFromFile {

	public static void main(String[] args) throws Exception {

		FileInputStream fis = new FileInputStream("mybean.xml");
		BufferedInputStream bis = new BufferedInputStream(fis);
		XMLDecoder xmlDecoder = new XMLDecoder(bis);
		MyBean mb = (MyBean) xmlDecoder.readObject();
		System.out.println(mb.isMyBoolean());
		System.out.println(mb.getMyString());
		for (String str : mb.getMyVector()) {
			System.out.println(str);
		}

	}

}

The MyBean class is shown here:

MyBean.java

package example;

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

public class MyBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private boolean myBoolean;
	private String myString;
	private Vector<String> myVector;

	public MyBean() {
	}
	public boolean isMyBoolean() {
		return myBoolean;
	}
	public void setMyBoolean(boolean myBoolean) {
		this.myBoolean = myBoolean;
	}
	public String getMyString() {
		return myString;
	}
	public void setMyString(String myString) {
		this.myString = myString;
	}
	public Vector<String> getMyVector() {
		return myVector;
	}
	public void setMyVector(Vector<String> myVector) {
		this.myVector = myVector;
	}
}

(Continued on page 2)

Page:    1 2 >