2016-01-26 53 views
0

我試圖解析一些XML到對象(Android)並使用Simple XML庫。我可以分析在它不過分的元素或列出了一些元素,當元素與相同的標記名稱的多個子元素,然後我得到一個錯誤:如何從具有多個相同名稱的子元素的元素中解析對象。 (SimpleXML Android)

元素「項目」已經與....

使用

我知道同名的標籤是一個問題,但每個標籤都有另一個屬性,因此可以用來解決這個問題。我嘗試了很多,但我無法完成它。

<Channel> 
    <News> 
     <Item kind="dunno"> 
       //An string with some info 
     </Item> 
     <Item kind="anotherOne"> 
       //An string with some info 
     </Item> 
     <Item kind="Pfff"> 
       //An string with some info 
     </Item> 
    </News> 
</Channel> 

我tryed如下:

@Root(name = "Channel") 
public class Channel 
    { 
     @Elementlist(inline = true) 
     private List<News> news; 
    } 


@Root(name = "News") 
public class News 
    { 
     @Element(name = item) 
     private String item; 
    } 

但是,這並不工作becouse項alrealy使用。我嘗試將其列入清單,但也無效。我嘗試了更多的東西,但正如我所說,我無法完成它。

我想要的是我有一個Chanel對象,其中包含一個或多個新聞對象,其中包含一個或多個項目。有時候只有一件物品,有時候還有更多。

回答

0

我找到了解決方案。 Insead

@Root(name = "Channel") 
public class Channel 
{ 
    @Elementlist(inline = true) 
    private List<News> news; 
} 

我需要做的是理解。

@Root(name = "Channel") 
public class Channel 
{ 
    @Elementlist(name="News") 
    private List<String> news; 
} 

該庫自動識別項目並將其添加到列表中。整個新聞類不再是必需的。

相關問題