Domanda

Im trying to parse a RSS feed with simple-XML that contains an Element that is declared twice in the XML. In there is a element that is declared with a namespace and without a namespace. I cant seem to get the Annotations right that both elements will be correctly deserialized. I tried the various ways of solving this with answers described in the question Simple Xml - Element Declared Twice Error, it seems like more people have suffered from this problem but I haven't been able to find a solution on the interwebs

The XML feed I want to parse is:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>Lorem ipsum dolor sit amet</title>
        <atom:link href="... FEED URL ..." rel="self" type="application/rss+xml" />
        <link>Lorem ipsum dolor sit amet</link>
        <description>Lorem ipsum dolor sit amet</description>
        <lastBuildDate>Sun, 29 Sep 2013 15:26:59 +0000</lastBuildDate>
        <language>nl-NL</language>
        <sy:updatePeriod>hourly</sy:updatePeriod>
        <sy:updateFrequency>1</sy:updateFrequency>
        <generator>http://wordpress.org/?v=3.6.1</generator>
        <item>
            <title>Lorem ipsum dolor sit amet</title>
            <link>... URL ..</link>
            <comments>... URL ..</comments>
            <pubDate>Sun, 29 Sep 2013 08:08:13 +0000</pubDate>
            <dc:creator>... AUTHOR ...</dc:creator>
            <category><![CDATA[... Category ...]]></category>
            <guid isPermaLink="false">... URL ..</guid>
            <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse in purus nunc. Nulla id lorem eget sem convallis bibendum.</description>
            <content:encoded>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse in purus nunc. Nulla id lorem eget sem convallis bibendum.</content:encoded>
            <wfw:commentRss>... URL ..</wfw:commentRss>
            <slash:comments>4</slash:comments>
        </item>
    </channel>
</rss>

The Java classes I'm using with annotations:

@Root
public class Rss {
    @Element(name = "channel")
    public Channel mChannel;

    @Attribute
    public String version;
}


@NamespaceList({
        @Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom"),
        @Namespace(reference = "http://purl.org/rss/1.0/modules/syndication/", prefix = "sy")
})
public class Channel {
    @Element(name = "title")
    public String title;

    @ElementList(entry = "link", inline = true, required = false)
    public List<Link> links;

    @Element(name = "description")
    public String description;

    @Element(name = "lastBuildDate")
    public String lastBuildDate;

    @Element(name = "language", required = true)
    public String language;

    @Element(name = "updatePeriod")
    public String updatePeriod;

    @Element(name = "updateFrequency")
    public int updateFrequency;

    @Element(name = "generator")
    public String generator;

    @ElementList(name = "item", required = true, inline = true)
    public List<Item> items;
}

@NamespaceList({
        @Namespace(prefix = "content", reference = "http://purl.org/rss/1.0/modules/content/"),
        @Namespace(prefix = "wfw", reference = "http://wellformedweb.org/CommentAPI/"),
        @Namespace(prefix = "dc", reference = "http://purl.org/dc/elements/1.1/"),
        //@Namespace(reference = "http://purl.org/rss/1.0/modules/slash/", prefix = "slash")
})
public class Item {
    @Element(name = "title")
    public String title;

    @Element(name = "link")
    public String link;

    @Element(name = "guid")
    public String guid;

    @ElementList(inline = true, name = "comments", required = false)
    public Comments comments;

    @Element(name = "pubDate")
    public String pubDate;

    @ElementList(name = "category", inline = true)
    public List<Category> categories;

    @Element(name = "creator")
    public String creator;

    @Element(name = "description")
    public String description;

    @Element(name = "encoded", data=true)
    public String encoded;

    @Element(name = "commentRss")
    public String commentRss;

    @Override
    public String toString() {
        return title + "\n" + creator + "\n" + pubDate + "\n";
    }

    private class Comments {
        @Text(required = false)
        public String comment;
    }
}
È stato utile?

Soluzione

I have a similar problem with the category elements

I create a new class like this:

@Root(name = "category", strict = false)
class Category {
    @Text
    public String text;
}

and it works...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top