当前位置: 首页 > 面试题库 >

使用“元素已被使用”解析XML提要模具

江雅懿
2023-03-14
问题内容

我正在尝试使用Android中的SimpleXML解析XML提要:http
:
//backend.deviantart.com/rss.xml?
type=deviation&q=by%3Aspyed+sort%3Atime+meta%
3Aall

这里的示例:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title>DeviantArt: spyed's</title>
        <link>http://www.deviantart.com/?order=5&amp;q=by%3Aspyed</link>
        <description>DeviantArt RSS for sort:time by:spyed</description>
        <language>en-us</language>
        <copyright>Copyright 2015, DeviantArt.com</copyright>
        <pubDate>Thu, 20 Aug 2015 07:45:31 PDT</pubDate>
        <generator>DeviantArt.com</generator>
        <docs>http://blogs.law.harvard.edu/tech/rss</docs>
        <atom:icon>http://st.deviantart.net/minish/touch-icons/android-192.png</atom:icon>
        <atom:link type="application/rss+xml" rel="self" href="http://backend.deviantart.com/rss.xml?q=sort%3Atime+by%3Aspyed&amp;type=deviation" />
        <atom:link rel="next" href="http://backend.deviantart.com/rss.xml?q=sort%3Atime+by%3Aspyed&amp;type=deviation&amp;offset=60" />
        <item>
            <title>Happy 15th Birthday!</title>
            <link>http://spyed.deviantart.com/journal/Happy-15th-Birthday-552416478</link>
            <guid isPermaLink="true">http://spyed.deviantart.com/journal/Happy-15th-Birthday-552416478</guid>
            <pubDate>Sun, 09 Aug 2015 01:41:54 PDT</pubDate>
            <media:title type="plain">Happy 15th Birthday!</media:title>
            <media:keywords />
            <media:rating>nonadult</media:rating>
            <media:category label="Personal">journals/personal</media:category>
            <media:credit role="author" scheme="urn:ebu">spyed</media:credit>
            <media:credit role="author" scheme="urn:ebu">http://a.deviantart.net/avatars/s/p/spyed.gif</media:credit>
            <media:copyright url="http://spyed.deviantart.com">Copyright 2015 spyed</media:copyright>
            <media:description type="html" />
            <media:content url="http://spyed.deviantart.com/journal/Happy-15th-Birthday-552416478" medium="document" />
            <media:text type="html">In April I wrote a post reflecting on my 15 years at DeviantArt, since the project began in April of 2000 and was launched to the public on August 7th. I went back and re-read what I wrote, and wow, speaking of audacious I'm pretty hardcore in this post. I write from my heart sometimes and then I hit post and that's that. So for our official 15th Birthday I wanted to re-share a link to that post:&lt;br /&gt;&lt;br /&gt;And then today, I wrote this reflection for you as a toast &amp;amp; thanks for all the llamas:&lt;br /&gt;&lt;br /&gt;Happy 15th Birthday DeviantArt! We are evolving, and soon we will be able to share exactly how you can be a part of our transformation, ensuring that the De</media:text>
            <description />
        </item>
        <item>
            <title>The Floor is Open</title>
            <link>http://spyed.deviantart.com/journal/The-Floor-is-Open-548845475</link>
            <guid isPermaLink="true">http://spyed.deviantart.com/journal/The-Floor-is-Open-548845475</guid>
            <pubDate>Sat, 25 Jul 2015 11:38:46 PDT</pubDate>
            <media:title type="plain">The Floor is Open</media:title>
            <media:keywords />
            <media:rating>nonadult</media:rating>
            <media:category label="Personal">journals/personal</media:category>
            <media:credit role="author" scheme="urn:ebu">spyed</media:credit>
            <media:credit role="author" scheme="urn:ebu">http://a.deviantart.net/avatars/s/p/spyed.gif</media:credit>
            <media:copyright url="http://spyed.deviantart.com">Copyright 2015 spyed</media:copyright>
            <media:description type="html" />
            <media:content url="http://spyed.deviantart.com/journal/The-Floor-is-Open-548845475" medium="document" />
            <media:text type="html">Happy Saturday! I'm hanging out at home, so I thought I'd have an open conversation. Post anything, I'll reply. It's about 11:30am, I'll be in front of this thing for two hours before I go to my god daughters 7th birthday party! Yay!&lt;br /&gt;&lt;br /&gt;If you're wondering what we're up to, check out the DeviantArt Timeline ( http://www.deviantart.com/timeline/ ) -- It might compel you to ask some questions. Or, if there's anything you've been wondering about, ask!&lt;br /&gt;&lt;br /&gt;Here's some food for thought to get you started about the overall state of DeviantArt:&lt;br /&gt;&lt;br /&gt;Our team focuses on two things that matter most to us, the first is our mission to Entertain, Inspire &amp;amp; Em</media:text>
            <description />
        </item>
        ... etc ....

但出现以下错误:

08-13 15:55:38.529: W/System.err(1437): org.simpleframework.xml.core.PersistenceException: Element 'title' is already used with @org.simpleframework.xml.Element(data=false, name=title, required=false, type=void) on field 'title' public java.lang.String com.example.feedreader.SimpleRss$Channel$RssItem.title at line 1

我注意到title在两个不同级别都有一个标签,但是我不确定如何在代码中进行区分。

这是我所拥有的:

@Root(name = "rss", strict = false)
public class SimpleRss implements FeedRoot {

    @Root(name = "channel")
    public static class Channel {

        @Element(name = "title", required = false)
        public String title = "";

        @Attribute(name = "href", required = false)
        @ElementList(entry = "link", inline = true, required = false)
        private List<String> links;

        @Element(name = "description", required = false)
        @Namespace(prefix = "")
        public String description = "";

        @Element(name = "pubDate", required = false)
        public String pubDate = "";

        @Element(name = "event_link", required = false)
        public String event_link = "";

        @Root(name = "item")
        public static class RssItem implements SimpleFeedItem {

            public String parent = "";
            public String feedUrl = null;
            public Date date = null;

            @Element(name = "title", required = false)
            public String title = "";

            @Element(name = "link", required = false)
            public String link = "";

            @Element(name = "event_link", required = false)
            public String event_link = "";

            @Element(name = "description", required = false)
            public String description = "";

            @Element(name = "pubDate", required = false)
            public String pubDate = "";

            @Path("enclosure")
            @Attribute(name = "url", required = false)
            public String enclosureUrl = "";

            @Path("enclosure")
            @Attribute(name = "type", required = false)
            public String enclosureType = "";

            @Override
            public String getTitle() {
                return title;
            }

            @Override
            public String getDescription() {
                return description;
            }

            @Override
            public String getLink() {
                return link;
            }

            @Override
            public String getEventLink() {
                return event_link;
            }

            @Override
            public String getAdditional() {
                return parent;
            }

            @Override
            public String getPubDate() {
                return pubDate;
            }

            @Override
            public String getFeedUrl() {
                return feedUrl;
            }

            @Override
            public void setFeedUrl(String url) {
                feedUrl = url;
            }

            @Override
            public void setDate(Date date) {
                this.date = date;
            }

            @Override
            public Date getDate() {
                return date;
            }

            @Override
            public String getPictureUrl() {
                if (DataUtils.isEmpty(enclosureType)) {
                    return null;
                } else if (SimpleFeedItem.SupportedFeedImageTypes
                        .contains(enclosureType.toLowerCase(Locale.ENGLISH))) {
                    return enclosureUrl;
                } else {
                    return null;
                }
            }

        }

        @ElementList(inline = true, required = false)
        public List<RssItem> rssItems = J.newArrayList();

    }

    @Element(name = "channel")
    public Channel channel;

    @Override
    public List<SimpleFeedItem> getItems() {
        List<SimpleFeedItem> items = J.newArrayList();

        if (channel != null && channel.rssItems != null) {
            for (RssItem item : channel.rssItems) {
                String title = channel.title;
                if (title == null) {
                    title = "";
                }
                item.parent = title.intern();
                items.add(item);
            }
        }
        return items;
    }

    @Override
    public String getTitle() {
        if (channel == null) {
            return "";
        } else {
            return channel.title;
        }
    }

}

我尝试注释掉

@Element(name = "title", required = false)
public String title = "";

Channel课堂上,我得到同样的错误。另外,如果我只是重命名它。

RssItem但是,如果我对班上的那个做同样的事情,它可以工作,但我没有这些项目的标题。

谢谢!


这样可以解决问题:

删除此:

@Element(name = "title", required = false)

用。。。来代替:

//inside the inner class RssItem
@Path("title")
@Text(required=false)
public String title = "";

对于任何其他获得异常的字段,都应重复此操作。



 类似资料:
  • 问题内容: 我目前正在使用SAX(Java)来解析少量不同的XML文档,每个文档代表不同的数据,并且结构略有不同。因此,每个XML文档都由不同的SAX类(子类)处理。 但是,在所有这些不同的文档中可能会出现一些XML结构。理想情况下,我想告诉解析器“嘿,当你到达一个元素,只要使用阅读它,并给我回的结果。如果你达到,使用读它,并给我回的是结果”。 但是,我看不到这样做的明显方法。 我是否应该只制作一

  • 根据这一点,像XML这样的 但是,将Jackson XmlMapper(默认情况下)用于此类类: 给出一个错误: com.fasterxml.jackson.databind.exc.未识别属性异常:未识别字段""(类org.example.jackson.unwrap.TestIt1$根),未标记为可忽略(一个已知属性:"sub"])... 好的,我可以补充一下 除根文本“asdasda”外,我

  • 我目前正在学习如何在android中使用Jaxb解析xml文件。但是我不知道代码中有什么错误,以及在哪里和如何纠正它。我无法解析xml并获得食品列表。如果我删除List并简单地把它写成Food,那么只有xml中的最后一个元素被解析,其余的似乎都被覆盖了。请帮助我。 我试图解析http://www.w3schools.com/xml/simple.xml,,目前我有这样的代码: ---- 用于取消

  • 我试图从这个表中解析数据。例如,假设我想解析第二行中的第二个元素(称为SLO)。 我可以看到TR里面有一个TR,而SLO这个词甚至没有ID或任何东西。我如何解析这个? 这是代码: 我不知道在文档中放什么。选择(“”);因为我从未解析过这样的东西。我只解析过网页标题之类的东西。有人能帮我吗?

  • 问题内容: 在编辑xml时,java的dom如何删除doctype? 得到了这个xml文件: 我的功能很基本: 它正在工作,但是文档类型被删除了!我只得到了整个文档,却没有doctype部分,这对我来说很重要,因为它允许我按id进行检索!我们如何保留doctype?为什么会擦除它?我用outputkeys或omImpl.createDocumentType尝试了许多解决方案,但这些都不起作用… 谢

  • 这是我的口袋碎片。爪哇: 通常,我在异步任务中使用jsoup解析一些数据。现在,我需要解析另一个应该包含元素或w/e的url,例如。 我可以在同一个异步任务中完成它,还是应该创建一个新的异步任务? 基本上我需要我的url是: 然后我应该解析一些其他元素,放入数组列表,然后使用它。 是否可能,像多个连接和使用一个元素的url? 我正在解析以下网站:,我正在解析异步任务中的href标记,比如: