2014-10-09 75 views
1

<Images>標記中,this API call返回不同類型的混合:fanart,boxart,banner,screenshot,clearlogo。使用SimpleXML解析混合列表

使用SimpleXML框架,將這些解析到單獨的列表中的最佳方法是什麼?我想要一個List<FanArt>,List<BoxArt>,List<Banner>等網站上的examples似乎不包括這種情況。我已經摸索了幾個不同的想法,但我甚至不確定SimpleXML框架能夠以一種簡單的方式處理這個問題。

例如,下面拋出此異常:org.simpleframework.xml.core.PersistenceException:名現場 'clearLogos' '形象' 的重複標註.....

@ElementList(name = "Images", entry = "clearlogo") 
private List<ClearLogo> clearLogos; 

@ElementList(name = "Images", entry = "boxart") 
private List<BoxArt> boxart; 

回答

1

在遇到任何人遇到這種情況,需要某種解決方案,我現在已經完成了這個工作。這份工作是否完成,但這不是我所追求的。

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