java - 简单的 XML 序列化程序无法满足元素

标签 java android xml parsing serialization

我是这个简单 XML 序列化程序的新手,我想解析 IGN 新闻提要。问题是我收到以下错误:

Unable to satisfy @org.simpleframework.xml.Element(data=true, name=description, required=true, type=void) on field 'description' private java.lang.String org.android.entities.Channel.description for class org.android.entities.Channel at line 2

xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.ign.com/~d/styles/itemcontent.css"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
   <channel>
      <title>IGN All</title>
      <description>The latest IGN news, reviews and videos about video games, movies, TV, tech and comics</description>
      <link>http://pipes.yahoo.com/pipes/pipe.info?_id=5985e93c1e0bc73949d56890f4462756</link>
      <atom:link rel="next" href="http://pipes.yahoo.com/pipes/pipe.run?_id=5985e93c1e0bc73949d56890f4462756&amp;_render=rss&amp;page=2" />
      <pubDate>Wed, 17 Oct 2012 20:05:24 +0000</pubDate>
      <generator>http://pipes.yahoo.com/pipes/</generator>
      <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.ign.com/ign/all" /><feedburner:info uri="ign/all" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.ign.com%2Fign%2Fall" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.ign.com%2Fign%2Fall" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><item>
         <title>Young Justice: "Before the Dawn" Review</title>
         <link>http://feeds.ign.com/~r/ign/all/~3/-l_luafUGXM/young-justice-before-the-dawn-review</link>
         <description>DC Nation has been pulled for now, but we've seen the one last episode of Young Justice that sneaked through: "Before the Dawn" reviewed!</description>
         <guid isPermaLink="false">507deab69e4e6be8947d4a79</guid>
         <pubDate>Wed, 17 Oct 2012 19:32:42 +0000</pubDate>
         <content:encoded><![CDATA[<p><strong>Full superhero sidekick spoilers follow.</strong>
</p><p>Have you heard the news, folks? <a rel="nofollow" target="_blank" href="http://www.ign.com/articles/2012/10/15/new-young-jusice-green-lantern-episodes-abruptly-pushed-to-2013">Cartoon Network abruptly pulled its DC Nation block of programming</a> -- which includes Young Justice and Green Lantern: The Animated Series -- this past weekend, leaving fans confused (and then angry) by the sudden, unexplained move. But what seems to have been an eleventh-hour decision means that the episodes that were originally scheduled to air on Saturday were still made available on iTunes for legal purchase (probably because someone forgot to pull them too). Which means we have this one last review for you before yet another Young Justice hiatus begins…
</p><p><a rel="nofollow" target="_blank" href="http://www.ign.com/articles/2012/10/17/young-justice-before-the-dawn-review">Continue reading&hellip;</a></p>]]></content:encoded>
         <media:content height="341" type="image/jpeg" url="http://oyster.ignimgs.com/wordpress/stg.ign.com/2012/10/robin_batgirl_young_justice.jpg" width="610" />
      <feedburner:origLink>http://feeds.ign.com/~r/ign/articles/~3/ts68IIyx4XI/young-justice-before-the-dawn-review</feedburner:origLink></item>
...
</channel>
</rss><!-- fe1.yql.bf1.yahoo.com compressed/chunked Wed Oct 17 20:05:23 UTC 2012 -->

因此解析器说问题出在“ channel ”标签中的“描述”标签中。我真的不知道我做错了什么。这是我的 channel 类:

package org.android.entities;

import java.io.Serializable;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;


    @Root public class Channel implements Serializable{


    /**
     * 
     */
    private static final long serialVersionUID = -6866019353714061968L;

    public Channel() {
        // TODO Auto-generated constructor stub
    }

    @Element
    private String title;

    @Element
    private String description;

    @Element
    private String link;

    @Element
    private String pubDate;

    @ElementList
    private List<Item> item;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public List<Item> getNews() {
        return item;
    }

    public void setNews(List<Item> news) {
        this.item = news;
    }
}

这是我执行反序列化的方法:

private void getFromCache() {
        Serializer serializer = new Persister();
        try {
            data = serializer.read(Channel.class, destinationFile, false);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
    }

最佳答案

现在回答我自己的问题。我玩了一点,问题就像 Nathan Villaescusa 在忽略 RSS 标签时建议的那样。看起来您必须创建一个必须是 xml 文件的根元素的类(在本例中为 RSS 类,它只有一个名为 Channel 的元素)。我没有找到可以忽略该元素并直接跳转到 channel 标签的解决方案。感谢您的帮助:)

关于java - 简单的 XML 序列化程序无法满足元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12942866/

相关文章:

java - 创建一个随机的 4 位数字,并将其存储到一个字符串中

c# - 如何在xml元素内添加冒号? Linq to xml C#

Java监视器实现

c# - 如何使用 C# 测量内存使用情况(就像我们在 Java 中所做的那样)?

java - 使用 Jenkins 执行构建

android - 如何从android中的微调器列表中隐藏所选项目

android - 可绘制对象中不能有多个文件夹吗?

java - 在折叠栏布局android中向上滚动时淡化整个布局

c# - 将 XML 反序列化为对象 C#

java - 谜语的乐趣