2014-10-09 3 views
1

태그 내에는 fanart, boxart, banner, screenshot, clearlogo와 같은 다양한 유형의 조합이 반환됩니다.혼합 목록을 SimpleXML로 구문 분석

SimpleXML Framework를 사용하여이를 개별 목록으로 파싱하는 가장 좋은 방법은 무엇입니까? List<FanArt>, List<BoxArt>, List<Banner> 등을 원합니다. 사이트의 examples은이 상황을 다루지 않는 것 같습니다. 나는 몇 가지 다른 아이디어로 더듬었지만, SimpleXML Framework가이를 직접적으로 처리 할 수 ​​있는지 확신 할 수 없다. 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

를 예를 들면

은 아래에이 예외를 throw 누구나이 문제를 겪고 어떤 종류의 해결책이 필요하다. 그 일을하고 있긴하지만 실제로 내가 한 일이 아니야.

@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; 
} 
} 
관련 문제