gpt4 book ai didi

java - 简单 XML 解析 RSS 提要,其中包含声明两次的 2

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:54 25 4
gpt4 key购买 nike

我正在尝试使用包含在 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;
}
}

最佳答案

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

我像这样创建一个新类:

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

它有效...

关于java - 简单 XML 解析 RSS 提要,其中包含声明两次的 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com