草庐IT

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

coder 2023-12-16 原文

我是这个简单 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 - 简单的 XML 序列化程序无法满足元素的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  6. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  7. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  8. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  9. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  10. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

随机推荐