4

我试图用包含在 XML 中声明两次的元素的简单 XML 解析 RSS 提要。其中有一个元素声明了命名空间但没有命名空间。我似乎无法正确地获得两个元素都将被正确反序列化的注释。我尝试了解决此问题的各种方法,其中包含问题Simple Xml - Element Declared Twice Error中描述的答案,似乎更多的人遇到了这个问题,但我无法在互联网上找到解决方案

我要解析的 XML 提要是:

<?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>

我与注释一起使用的 Java 类:

@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;
    }
}
4

1 回答 1

1

我对类别元素有类似的问题

我创建了一个这样的新类:

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

它有效......

于 2017-11-27T00:35:14.337 回答