beans

Bean XML deserialization

With this example we are going to demonstrate how to deserialize a java Bean using the XMLDecoder. The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream. In short, to deserialize a java Bean using the XMLDecoder you should:

  • Create an xml representation of the java bean.
  • Create a simple class, like Bean class in the example. It has two String properties and getters and setters for the properties. It has the same properties as the xml doument.
  • Create a new XMLDecoder, with a new BufferedInputStream that is created reading a new FileInputStream by opening a connection to the xml file.
  • Use the readObject() API method of XMLDecoder to read the next object from the underlying input stream. The result is an object of Bean class.
  • Use close() API method to close the input stream associated with this stream.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

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

public class BeanXMLDeserialization {
	
	public static void main(String[] args) throws Exception {
		
		XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("in.xml")));

		// Deserialize object from XML
		Bean bean = (Bean) decoder.readObject();
		decoder.close();
		
		System.out.println("Propert1 value: " + bean.getProperty1());
		System.out.println("Propert2 value: " + bean.getProperty2());

	}
	
	public static class Bean {
		
		// Property property1
		private String property1;
	    // Property property2
		private int property2;
	    
		public String getProperty1() {
			return property1;
		}
		public void setProperty1(String property1) {
			this.property1 = property1;
		}
		
		public int getProperty2() {
			return property2;
		}
		public void setProperty2(int property2) {
			this.property2 = property2;
		}
	    
	}

}

Output:

Propert1 value: value1
Propert2 value: 2

Input:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_05" class="java.beans.XMLDecoder"> 
 <object class="com.javacodegeeks.snippets.core.BeanXMLDeserialization$Bean"> 
  <void property="property1"> 
   <string>value1</string> 
  </void> 
  <void property="property2"> 
   <int>2</int> 
  </void> 
 </object> 
</java>

 
This was an example of how to deserialize a java Bean using the XMLDecoder in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button