【问题标题】:How to parse an object from an element with multiple, same named sub elements. (SimpleXML Android)如何从具有多个同名子元素的元素中解析对象。 (SimpleXML 安卓)
【发布时间】:2016-01-26 12:33:59
【问题描述】:

我尝试将一些 XML 解析为对象 (Android) 并使用 Simple XML 库。我可以解析一些带有子元素或列表的元素,但是当一个元素有多个具有相同标签名称的子元素时,我会收到错误:

元素 'Item' 已与...一起使用。

我知道同名标签是一个问题,但每个标签都有另一个属性,因此可以用来解决这个问题。我尝试了很多,但我无法完成。

<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>

我尝试了以下方法:

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

但这不起作用,因为项目已经使用过。我试图将其列为列表,但也不起作用。我尝试了更多的东西,但正如我所说,我无法完成它。

我想要的是我有一个 Chanel 对象,其中包含一个或多个包含一个或多个项目的新闻对象。有时只有 1 项,有时还有更多。

【问题讨论】:

    标签: android xml xml-parsing simple-framework


    【解决方案1】:

    我找到了解决方案。

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

    图书馆会自动识别项目并将它们添加到列表中。整个新闻类不再是必需的。

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多