在<Images>
代码中,this API call会返回不同类型的混合:fanart,boxart,banner,screenshot,clearlogo。
使用SimpleXML Framework,将这些解析为单独的列表的最佳方法是什么?我想要List<FanArt>
,List<BoxArt>
,List<Banner>
等。网站上的examples似乎并未涵盖这种情况。我已经摸索了一些不同的想法,但我甚至不确定SimpleXML Framework是否可以直接处理这个问题。
例如,下面引发了这个异常: org.simpleframework.xml.core.PersistenceException:在字段'clearLogos'上复制名称'Images'的注释.....
@ElementList(name = "Images", entry = "clearlogo")
private List<ClearLogo> clearLogos;
@ElementList(name = "Images", entry = "boxart")
private List<BoxArt> boxart;
答案 0 :(得分:2)
如果有人遇到这种情况并需要某种解决方案,我现在就已经完成了这项工作。这份工作,但它不是我真正想要的。
@Root
public class Images {
@ElementListUnion({
@ElementList(entry = "fanart", type = FanArt.class, inline = true),
@ElementList(entry = "boxart", type = BoxArt.class, inline = true),
@ElementList(entry = "screenshot", type = ScreenShot.class, inline = true),
@ElementList(entry = "banner", type = Banner.class, inline = true),
@ElementList(entry = "clearlogo", type = ClearLogo.class, inline = true)
})
private List<Object> images;
private List<FanArt> fanarts;
private List<BoxArt> boxarts;
private List<ScreenShot> screenshots;
private List<Banner> banners;
private List<ClearLogo> clearlogos;
public List<FanArt> getFanarts() {
if (fanarts == null) {
fanarts = new ArrayList<FanArt>();
for (Object image : images) {
if (image instanceof FanArt) {
fanarts.add((FanArt) image);
}
}
}
return fanarts;
}
public List<BoxArt> getBoxarts() {
if (boxarts == null) {
boxarts = new ArrayList<BoxArt>();
for (Object image : images) {
if (image instanceof BoxArt) {
boxarts.add((BoxArt) boxarts);
}
}
}
return boxarts;
}
public List<ScreenShot> getScreenshots() {
if (screenshots == null) {
screenshots = new ArrayList<ScreenShot>();
for (Object image : images) {
if (image instanceof ScreenShot) {
screenshots.add((ScreenShot) image);
}
}
}
return screenshots;
}
public List<Banner> getBanners() {
if (banners == null) {
banners = new ArrayList<Banner>();
for (Object image : images) {
if (image instanceof Banner) {
banners.add((Banner) image);
}
}
}
return banners;
}
public List<ClearLogo> getClearLogos() {
if (clearlogos == null) {
clearlogos = new ArrayList<ClearLogo>();
for (Object image : images) {
if (image instanceof ClearLogo) {
clearlogos.add((ClearLogo) image);
}
}
}
return clearlogos;
}
}