开发者

Read XML file using SimpleXML throws Serializer Exception

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜