Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleXML, org.simpleframework.xml.core.PersistenceException

After a lot of frustration I have come here for help. I am using org.simpleframework.xml to parse RSS feeds in Android. I get the following error when I try to parse the xml file:

org.simpleframework.xml.core.PersistenceException: Element 'link' is already used with @org.simpleframework.xml.Element(data=false, name=, required=true, type=void) on field 'link'

This is a sample of the xml I am trying to parse:

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xml:base="http://www.somelink.com/" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>My Title</title>
        <description>Latest breaking news, sport, weather and opinion, from South Africa, Africa and the world.</description>
        <link>http://www.somelink.com/</link>
        <atom:link rel="self" href="http://www.someotherlink.com" />
    </channel>
</rss>

These are the classes and annotations that I use in the code:

@Root(name = "rss")
@Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom")
public class NewsFeed
{
    @Version(revision = 2.0)
    private float version;

    @Attribute
    private String base;

    @Element
    private Channel channel;
}

@Root(name = "channel")
public class Channel
{
    @Element
    private String title;

    @Element
    private String description;

    @Element
    private String link;

    @Element
    @Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom")
    private Link rssLink;

    @ElementList(inline = true)
    private List<NewsItem> newsItems;
}

@Root(name = "link")
public class Link
{
    @Attribute
    private String rel;

    @Attribute
    private String href;

    public String getRel()
    {
        return rel;
    }

    public void setRel(String rel)
    {
        this.rel = rel;
    }

    public String getHref()
    {
        return href;
    }

    public void setHref(String href)
    {
        this.href = href;
    }
}

I created the Link class so that I could use the atom namespace. As you can see from the error, the library doesn't appear to be taking the namespace into account and that's why it's complaining that the link element has already been used. What am I doing wrong? Your help would be greatly appreciated.

like image 550
Wouter Avatar asked Nov 11 '22 04:11

Wouter


1 Answers

I'm facing the same problem, It seems link and atom:link is treated as same entry even in different namespace, and they need to be merged to one List<Link> rssLink list.

Here's my kotlin model code for parsing rss feed, it works fine:

@Root(name = "rss", strict = false)
data class RssFeed(
    @field:Element(name = "channel")
    var channel: RssChannel = RssChannel()
)

@NamespaceList(
    Namespace(prefix = "atom", reference = "http://www.w3.org/2005/Atom")
)
@Root(name = "channel", strict = false)
data class RssChannel(
    @field:Element var title: String = "",
    @field:Element var description: String = "",
    @field:Element var generator: String = "",
    @field:Element var lastBuildDate: String = "",

    @field:ElementList(entry = "link", required = false, inline = true)
    var links: List<RssLink> = ArrayList(),

    @field:ElementList(inline = true)
    var items: List<RssItem> = ArrayList()
)

@Root(name = "item", strict = false)
data class RssItem(
    @field:Element
    var title: String = "",

    @field:Element
    var pubDate: String = "",

    @field:Element
    var link: String = "",

    @field:Element(required = false)
    var description: String = "",

    @field:Element
    var guid: String = ""
)

@Root(name = "link", strict = false)
data class RssLink(
    // for <link>http://www.somelink.com/</link>
    @field:Text(required = false) var text: String = "",

    // for <atom:link rel="self" href="http://www.someotherlink.com" />
    @field:Attribute(required = false) var rel: String = "",
    @field:Attribute(required = false, name = "type") var contentType: String = "",
    @field:Attribute(required = false) var href: String = ""
)

You can also check out here

like image 175
tianyu Avatar answered Nov 14 '22 23:11

tianyu