我想使用 SimpleXML 将不同的 XML 序列化数据集反序列化为一个类(不同时) .
所有数据集的根类均为 <body/>
,但是不同的子类。有些有一个元素 <prediction>
, <prediction>
的其他多个元素。 <route>
相同。这是我当前使用的代码,但它给了我类型 PersistenceException
的错误(“重复注释”)。
解决这个问题最简单的方法是什么?任何帮助,将不胜感激。
@Root(name = "body", strict = false)
class NextBusAPIResult {
@Attribute(name = "copyright")
public String Copyright;
@Element(name = "predictions", required = false, type = Predictions.class)
public Predictions Predictions;
@ElementList(name = "predictions", required = false, inline = true)
public List<Predictions> PredictionsForMultiStops;
@Element(name = "route", required = false, type = RouteInfo.class)
public RouteInfo RouteInfo;
@ElementList(name = "route", required = false, inline = true)
public List<RouteSchedule> RouteSchedules;
}
最佳答案
解决此问题的最简单方法是始终使用多个元素。只需删除与单个元素相关的代码即可。
关于java - 从不同的 XML 数据集中反序列化具有相同名称的 XML @Element 和 @ElementList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203247/