简体   繁体   English

使用SimpleXML解析混合列表

[英]Parsing mixed list with SimpleXML

Within the <Images> tag, this API call returns a mix of different types: fanart, boxart, banner, screenshot, clearlogo. <Images>标记内, 此API调用返回不同类型的混合:fanart,boxart,banner,屏幕截图,clearlogo。

Using SimpleXML Framework, what is the best way to go about parsing those into separate Lists? 使用SimpleXML Framework,将它们解析为单独的List的最佳方法是什么? I'd like a List<FanArt> , List<BoxArt> , List<Banner> etc. The examples on the site do not seem to cover this situation. 我想要一个List<FanArt>List<BoxArt>List<Banner>等。该网站上的示例似乎并未涵盖这种情况。 I've fumbled about with a few different ideas, but I'm not even sure SimpleXML Framework can handle this in a straight forward way. 我已经摸索了一些不同的想法,但是我什至不确定SimpleXML Framework是否可以直接解决这个问题。

For example, the below throws this exception: org.simpleframework.xml.core.PersistenceException: Duplicate annotation of name 'Images' on field 'clearLogos'..... 例如,以下引发此异常: 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;

In case anyone comes across this and needs some sort of solution, I've done this for now to get it working. 万一有人遇到这种情况并需要某种解决方案,我现在已经做完了,以使其正常工作。 Does the job but it's not really what I was after. 做这份工作,但这不是我真正追求的。

@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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM