gpt4 book ai didi

java - SimpleXML,org.simpleframework.xml.core.PersistenceException

转载 作者:行者123 更新时间:2023-12-03 11:20:12 30 4
gpt4 key购买 nike

在经历了很多挫折之后,我来到这里寻求帮助。我正在使用 org.simpleframework.xml 来解析 Android 中的 RSS 提要。尝试解析 xml 文件时出现以下错误:

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'

这是我要解析的 xml 示例:
<?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>

这些是我在代码中使用的类和注释:
@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;
}
}

我创建了 Link 类,以便可以使用 atom 命名空间。正如您从错误中看到的那样,该库似乎没有考虑命名空间,这就是它提示链接元素已被使用的原因。我究竟做错了什么?您的帮助将不胜感激。

最佳答案

我也遇到了同样的问题,看来linkatom:link即使在不同的命名空间中也被视为相同的条目,它们需要合并到一个 List<Link> rssLink 列表。
这是我用于解析 rss 提要的 kotlin 模型代码,它工作正常:

@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 = ""
)
您也可以查看 here

关于java - SimpleXML,org.simpleframework.xml.core.PersistenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958312/

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