开发者

Read XML file using SimpleXML throws Serializer Exception

开发者 https://www.devze.com 2023-02-08 01:54 出处:网络
I use SimpleXML to save a simple POJO into XML file and then read it back. I follow this tutorial. The file开发者_StackOverflow is successfully created, but the reading part is just simply not working

I use SimpleXML to save a simple POJO into XML file and then read it back. I follow this tutorial. The file开发者_StackOverflow is successfully created, but the reading part is just simply not working. It throws the second exception which comes from serializer.read.

Room room = new Room("1");

Serializer serializer = new Persister();
try {
    File ff = new File("room.xml");
    serializer.write(room, ff);
} catch (FileNotFoundException e) {
    System.out.println("FileNotFoundException\n");
} catch (Exception e) {
    System.out.println("Exception from serializer.write\n");
}

try {
    File ffi = new File("room.xml");
    Room aroom = serializer.read(Room.class, ffi);
    System.out.println("RoomName: " + aroom.getRid() + "\n");
} catch (FileNotFoundException e) {
    System.out.println("FileNotFoundException\n");
} catch (Exception e) {
    System.out.println("Exception from serializer.read\n");
}

Any hint?


Make sure you have a default constructor in Room.

public Room(){
}

Alternatively, make sure your constructor looks like this:

public Room(@Attribute(name="rid") String rid){
    this.rid = rid;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消