XMLEncoder - 运行时异常

时间:2013-04-14 23:17:53

标签: java xml class object serialization

我一直在玩Java中的序列化-XML而且有点卡住了。当我运行这个程序时,我得到两个例外,我不确定原因是什么:

java.lang.InstantiationException: Ship
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Ship);
Continuing ...

我怀疑我试图序列化的类有问题,因为当我使用互联网的一个例子时它工作正常。

有人可以指出我犯了什么错误。

主:

public class Main {

    private static final String XMLLocation = "xmlTest.xml";
    static ObjectSerializationToXML serializer = new ObjectSerializationToXML();

    public Main() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Ship ship = new Ship("name", "324");
        serializer.serializeObjectToXML(XMLLocation, ship);

    }

}

对象序列化 - XML类:

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ObjectSerializationToXML {

    /**
     * <span id="IL_AD10" class="IL_AD">This method</span> saves (serializes) any java bean object into xml file
     */
    public void serializeObjectToXML(String xmlFileLocation,
            Object objectToSerialize) throws Exception {
        FileOutputStream os = new FileOutputStream(xmlFileLocation);
        XMLEncoder encoder = new XMLEncoder(os);
        encoder.writeObject(objectToSerialize);
        encoder.close();
    }

    /**
     * Reads Java Bean Object From XML File
     */
    public Object deserializeXMLToObject(String xmlFileLocation)
            throws Exception {
        FileInputStream os = new FileInputStream(xmlFileLocation);
        XMLDecoder decoder = new XMLDecoder(os);
        Object deSerializedObject = decoder.readObject();
        decoder.close();

        return deSerializedObject;
    }
}

要序列化的对象(导致异常的我的对象):

public class Ship {

    private String name;
    private String yearBuilt;


    public Ship(String name, String yearBuilt) {
        this.name = name;
        this.yearBuilt = yearBuilt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getYearBuild() {
        return yearBuilt;
    }
    public void setYearBuild(String yearBuild) {
        this.yearBuilt = yearBuild;
    }
    @Override
    public String toString() {
        return "ship [name=" + name + ", yearBuilt=" + yearBuilt + "]";
    }

}

要序列化的对象(例如来自可用的互联网):

public class MyBeanToSerialize {
    private String firstName;
    private String lastName;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

1 个答案:

答案 0 :(得分:0)

每当任何包含参数化构造函数并尝试序列化的类时,它都应该仅默认情况下由Constructor实例化。因此,XMLEncoder需要一个对象来默认构造函数来序列化它。

Ship类必须在包含参数化构造函数时实现默认构造函数,因为每当Ship类变为可序列化时,它都会寻找默认构造函数来实例化XMLEncoder。

请按以下方式查找更正的船级。

public class Ship {

private String name;
private String yearBuilt;


public Ship(String name, String yearBuilt) {
    this.name = name;
    this.yearBuilt = yearBuilt;
}

//Default constructor must be implemented for XMLEncoder serializing
public Ship() {
    super();
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public String getYearBuild() {
    return yearBuilt;
}
public void setYearBuild(String yearBuild) {
    this.yearBuilt = yearBuild;
}
@Override
public String toString() {
    return "ship [name=" + name + ", yearBuilt=" + yearBuilt + "]";
}

}