Java – Convert JavaBean object to XML and back using XMLEncoder and XMLDecoder

The classes XMLEncoder and XMLDecoder allows you to convert JavaBean objects to XML and back, respectively. Note, that they are specifically designed for JavaBeans specification. Using objects whose classes do not adhere to the specification may not be converted to XML accordingly.

Convert object to XML

This will created an XML file named StudentPersisted.xml

Convert XML to object

Given the following XML file, we’ll use XMLDecoder to convert the contents of the file to a JavaBean object.

Execute the following Java codes.

This outputs

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert