使用 SimpleXML 读取 XML 文件会引发序列化程序异常
我使用 SimpleXML 将简单的 POJO 保存到 XML 文件中,然后将其读回。我遵循此教程。文件已成功创建,但读取部分根本无法工作。它抛出来自 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");
}
有什么提示吗?
I use SimpleXML to save a simple POJO into XML file and then read it back. I follow this tutorial. The file 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保
Room
中有一个默认构造函数。或者,确保您的构造函数如下所示:
Make sure you have a default constructor in
Room
.Alternatively, make sure your constructor looks like this: