gpt4 book ai didi

java - 带有 xml 和 java 对象的 XStream

转载 作者:行者123 更新时间:2023-11-30 05:09:39 26 4
gpt4 key购买 nike

我有两个java类......

public class Item {
private int itemIndex;
private String containerType;
private Map<String, List<String>> contentType;
private String status;
private List<String> remark;
// their getters and setters
}

请告诉我如何将 Item 对象转换为 xml 以及将 xml 转换为 Item 对象?我使用 XStream jar 进行转换。我需要在 xml 中存储多个项目(项目列表)。请提供完整的 JAVA 编码,以将新项目添加到现有项目(存储在 xml 中)。

最佳答案

Sample code

ObjectOutputStream out = xstream.createObjectOutputStream(someWriter);

out.writeObject(new Person("Joe", "Walnes"));
out.writeObject(new Person("Someone", "Else"));
out.writeObject("hello");
out.writeInt(12345);

out.close();

关于java - 带有 xml 和 java 对象的 XStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3888253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com