I have been trying to wrap a set of classes based on Simple XML (Java Serializer) around a RSS Feed. The sample feed is
<?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>
The error that I am getting while running the code is
org.simpleframework.xml.core.PersistenceException: Element 'link' declared twice at line 24
And the error is fair enough because the particular element name occurs twice in the xml but in different ways.
The first link element is here
<link>http://www.codinghorror.com/blog/</link>
Its directly under the Channel tag. And then the next link tag is again under Channel in the following format
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />
In Channel.java class I cannot have two variables with the same name link. I tried changing a variable name to blogLink and tried giving name in the Element annotation and Eclipse gave me this error
Change was
@Element("name=link")
Result is
The attribute value is undefined for the annotation Element
I know I am missing something here but I am not able to put my finger on it. I would appreciate any help on this.
UPDATE
Channel Class
@Element(name="link")
@Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom")
private atomlink atomlink;
public atomlink getAtomLink() {
return atomlink;
}
Link Class
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;
}
}
I have changed the class names and yet it still points to the same error.
I have the same problem parsing the xml with content:
my code is:
The error with this is:
org.simpleframework.xml.core.PersistenceException: Duplicate annotation of name 'rating' on field 'ratingLike' public com.devicefms.android.boardgamesreview.beans.VideoXML$VideoEntry$LikeRating com.devicefms.android.boardgamesreview.beans.VideoXML$VideoEntry.ratingLike at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:258)
This is not a fix per-se, but I was able to work around this issue by replacing the 2 @Element entries for <atom:link/> and <link/> in my class with a single @ElementList, and creating an object that would satisfy both Link types. Something like this:
These are two different elements. They differ by namespace. See how can you map namespaces (if they are supported by that Simple XML at all).
Uh, found it in the doc:
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#namesoace
and so on.
Because the
link
element appears twice(though they have different namespace), you need to telllink
apart, blew is my whole answer:1)
Rss
bean2)
TestSimpleXmlConvert
methodHas this been resolved? Besides Mark's response (of using a collection) is there a proper way of doing this? How would we guard against other fields that weren't turned into collection become collections?
BTW I am working with RSS feeds.
The annotation you have listed is not formatted properly.
It should be
If an annotation has a single property named value, it can be assigned without specifying the key. But in this case, the property you are attempting to set is 'name' which has a value of type String.
The issue from the question is that the whole assignment of 'name' was enclosed in parenthesis, thus it was trying to set 'value' to be "name=link", which is why it was blowing up, because the @Element annotation doesn't specify a value property.