使用 SimpleXML 读取 XML 文件会引发序列化程序异常

发布于 2024-10-15 08:03:39 字数 921 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-10-22 08:03:39

确保 Room 中有一个默认构造函数。

public Room(){
}

或者,确保您的构造函数如下所示:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文