java - XML Serialization and Empty Constructors -


i have following class allows me serialize objects in program:

import java.beans.xmldecoder; import java.beans.xmlencoder; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream;  public class persistor<t> {      private t data;      public void save(t data, string file) {          try {              fileoutputstream os = new fileoutputstream(file);             xmlencoder encoder = new xmlencoder(os);             encoder.writeobject(data);             encoder.close();          } catch(filenotfoundexception e) {              system.out.println("file not found");          }      }      @suppresswarnings({ "unchecked", "finally" })     public t read(string file) {          try {              fileinputstream fis = new fileinputstream(file);             xmldecoder decoder = new xmldecoder(fis);             data = (t)decoder.readobject();             decoder.close();          } catch(filenotfoundexception e) {              system.out.println("file not found");          } {              return data;          }      }  } 

the thing have bussiness logic package classes student , seems need have empty constructor public student() {} program working:

package logic;  public class student {      private string name;      public student(string name) {          this.name = name;      } public student() {} // empty constructor      public string getname() {          return name;      }      public void setname(string name) {          this.name = name;      }      public string tostring() {          return name;      }  } 

if take out empty constructor, following exceptions appear on console:

java.lang.instantiationexception: logic.student continuing ... java.lang.illegalstateexception: outer element not return value continuing ... 

is there way fix stuff, mean not having empty constructor? because have 7 more classes needs have it's own empty constructor.

you can try constructorproperties annotation

public class student {     private string name;      @constructorproperties("name")     public student(string name) {         this.name = name;     }       public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     } } 

test

public class test1 {      public static void main(string[] args) throws exception {          student s1 = new student("jon");          xmlencoder encoder = new xmlencoder(new fileoutputstream("xxx"));          encoder.writeobject(s1);          encoder.close();           xmldecoder decoder = new xmldecoder(new fileinputstream("xxx"));          student s2 = (student)decoder.readobject();          decoder.close();          system.out.println(s2);     } } 

output

test.student@e3fd79 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -