java简单Xml元素声明两次错误
我一直在尝试将一组基于简单XML(Java序列化程序)的类包装在RSS提要中。样本提要是
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Coding Horror</title>
<link>http://www.codinghorror.com/blog/</link>
<description>programming and human factors - Jeff Atwood</description>
<language>en-us</language>
<lastBuildDate>Wed, 04 May 2011 20:34:18 -0700</lastBuildDate>
<pubDate>Wed, 04 May 2011 20:34:18 -0700</pubDate>
<generator>http://www.typepad.com/</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<image>
<title>Coding Horror</title>
<url>http://www.codinghorror.com/blog/images/coding-horror-official-logo-small.png</url>
<width>100</width>
<height>91</height>
<description>Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.</description>
<link>http://www.codinghorror.com/blog/</link>
</image>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />
</channel>
</rss>
我在运行代码时遇到的错误是
org.simpleframework.xml.core.PersistenceException: Element 'link' declared twice at line 24
这个错误很公平,因为特定的元素名在xml中出现了两次,但方式不同
第一个链接元素在这里
<link>http://www.codinghorror.com/blog/</link>
它就在频道标签的正下方。然后,下一个链接标签再次以以下格式显示在Channel下
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />
在频道。java类I不能有两个同名链接的变量。我尝试将一个变量名更改为blogLink,并尝试在元素注释中命名,Eclipse给了我这个错误
Change was
@Element("name=link")
Result is
The attribute value is undefined for the annotation Element
我知道我错过了一些东西,但我无法确定。如果你能帮我,我将不胜感激
更新
频道类
@Element(name="link")
@Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom")
private atomlink atomlink;
public atomlink getAtomLink() {
return atomlink;
}
链接类
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
@Root(name="link")
@Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom10")
public class atomlink {
@Attribute
private String rel;
public String getRel() {
return rel;
}
}
我已经更改了类名,但它仍然指向相同的错误
# 1 楼答案
您列出的批注格式不正确
应该是的
如果注释只有一个名为value的属性,则可以在不指定键的情况下指定该属性。但在本例中,您试图设置的属性是'name',其值为String类型
问题的问题是,‘name’的整个赋值被括在括号中,因此它试图将‘value’设置为“name=link”,这就是为什么它会爆炸,因为@Element注释没有指定value属性
# 2 楼答案
这个问题解决了吗?除了马克的反应(使用一个集合)之外,还有什么合适的方法吗?我们如何防止其他未转化为集合的字段变成集合
顺便说一句,我正在使用RSS源
# 3 楼答案
这本身并不是一个解决方案,但我通过替换<;atom:link/>;和<;链接/>;在我的类中使用一个@ElementList,并创建一个满足这两种链接类型的对象。比如: