I am working with SimpleXML library. I need to get rss feed and transform it to java object. Here the XML I need to map to the object
<rss xmlns:lj="http://www.livejournal.org/rss/lj/1.0/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Всегда! Везде! Со всеми! и ни с кем!</title>
<link>http://xuma.livejournal.com/</link>
<description>
Всегда! Везде! Со всеми! и ни с кем! - LiveJournal.com
</description>
<lastBuildDate>Thu, 17 Mar 2011 10:08:33 GMT</lastBuildDate>
<generator>LiveJournal / LiveJournal.com</generator>
<lj:journal>xuma</lj:journal>
<lj:journalid>5295048</lj:journalid>
<lj:journaltype>personal</lj:journaltype>
<copyright>NOINDEX</copyright>
<image>
<url>http://l-userpic.livejournal.com/104864030/5295048</url>
<title>Всегда! Везде! Со всеми! и ни с кем!</title>
<link>http://xuma.livejournal.com/</link>
<width>66</width>
<height>100</height>
</image>
<item>
<guid isPermaLink="true">http://xuma.livejournal.com/7831.html</guid>
<pubDate>Thu, 17 Mar 2011 10:08:33 GMT</pubDate>
<title>Япония. Поможем пострадавшим.</title>
<link>http://xuma.livejournal.com/7831.html</link>
<description>...</description>
<comments>http://xuma.livejournal.com/7831.html</comments>
<lj:security>public</lj:security>
<lj:reply-count>0</lj:reply-count>
</item>
<item>
<guid isPermaLink="true">http://xuma.livejournal.com/7575.html</guid>
<pubDate>Sat, 16 Oct 2010 12:36:48 GMT</pubDate>
<link>http://xuma.livejournal.com/7575.html</link>
<description>
<h1>Тест личности</h1><br /><a href="http://teh-nomad.livejournal.com/667478.html"><img border="0" src="http://www.ljplus.ru/img4/a/s/astro_nomad/chtree_6.jpg" alt="" /></a><br /><br /><a href="http://teh-nomad.livejournal.com/667478.html">Выбрать себе дерево</a>
</description>
<comments>http://xuma.livejournal.com/7575.html</comments>
<lj:music>I chase the Devil</lj:music>
<media:title type="plain">I chase the Devil</media:title>
<lj:mood>опаздываю...</lj:mood>
<lj:security>public</lj:security>
<lj:reply-count>0</lj:reply-count>
</item>
</channel>
</rss>
Here are beans I have defined:
@Root
public class RssType {
@Element(name="channel")
private ChannelType channel;
@Attribute
private String version;
public ChannelType getChannel() {
return channel;
}
public void setChannel(ChannelType channel) {
this.channel = channel;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
@Element(name="channel")
public class ChannelType {
@Element
private String title;
@Element
private String link;
@Element
private String description;
@Element
private String lastBuildDate;
@Element
private String generator;
@Element
@Namespace(reference="http://www.livejournal.org/rss/lj/1.0/", prefix="lj")
private String journal;
@Element
@Namespace(reference="http://www.livejournal.org/rss/lj/1.0/", prefix="lj")
private String journalid;
@Element
@Namespace(reference="http://www.livejournal.org/rss/lj/1.0/", prefix="lj")
private String journaltype;
@Element
private ImageType image;
@ElementList(inline=false)
private List<ItemType> item;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLastBuildDate() {
return lastBuildDate;
}
public void setLastBuildDate(String lastBuildDate) {
this.lastBuildDate = lastBuildDate;
}
public String getGenerator() {
return generator;
}
public void setGenerator(String generator) {
this.generator = generator;
}
public String getJournal() {
return journal;
}
public void setJournal(String journal) {
this.journal = journal;
}
public String getJournalid() {
return journalid;
}
public void setJournalid(String journalid) {
this.journalid = journalid;
}
public String getJournaltype() {
return journaltype;
}
public void setJournaltype(String journaltype) {
this.journaltype = journaltype;
}
public ImageType getImage() {
return image;
}
public void setImage(ImageType image) {
this.image = image;
}
public List<ItemType> getItem() {
return item;
}
public void setItem(List<ItemType> item) {
this.item = item;
}
}
@Element(name="image")
public class ImageType {
@Element
private String url;
@Element
private String title;
@Element
private String link;
@Element
private String width;
@Element
private String height;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
}
@Element(name="item")
public class ItemType {
@Element
private GuidType guid;
@Element
private String pubDate;
@Element
private String title;
@Element
private String link;
@Element
private String description;
@Element
private String comments;
@ElementList(inline=false)
private List<String> category;
@Element
@Namespace(reference="http://www.livejournal.org/rss/lj/1.0/", prefix="lj")
private String security;
@Element
@Namespace(reference="http://www.livejournal.org/rss/lj/1.0/", prefix="lj")
private String replyCount;
public GuidType getGuid() {
return guid;
}
public void setGuid(GuidType guid) {
this.guid = guid;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public List<String> getCategory() {
return category;
}
public void setCategory(List<String> category) {
this.category = category;
}
public String getSecurity() {
return security;
}
public void setSecurity(String security) {
this.security = security;
}
public String getReplyCount() {
return replyCount;
}
public void setReplyCount(String replyCount) {
this.replyCount = replyCount;
}
}
@Element(name="guid")
public class GuidType {
@Attribute
public String isPermaLink;
public String text;
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
public String getIsPermaLink() {
return isPermaLink;
}
public void setIsPermaLink(String isPermaLink) {
this.isPermaLink = isPermaLink;
}
}
Executing this code :
String xmlData = retrieve("http://zuma.livejournal.com/data/rss");
Serializer serializer = new Persister();
RssType example = serializer.read(RssType.class, xmlData);
Getting such exception
org.simpleframework.xml.core.AttributeException: Attribute 'isPermaLink' does not have a match in class com.ljclient.xml.beans.ItemType at line 22
But isPermaLink is not an attribute of <item> node but <guid> node.
If I made isPermaLink to be an attribute of <item> and in that case I’m getting following exception:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=false, name=, required=true, type=void) on field 'category' private java.util.List com.ljclient.xml.beans.ItemType.category for class com.ljclient.xml.beans.ItemType at line 22
Its strange because the value exist for each category node. Any ideas?
This is not a direct answer to your question, but have you tried creating
RssTypeinstance and writing it to XML. The point of this exercise is that then you can see how your markup translates to XML for any obvious issues.I know that this does not immediately solve the problem, but it should lead you to identifying what the problem is.
Okay I just tried:
And got:
I noticed that if a field is not set, I was getting and exception. So I simply set everything. Then I edited the xml to look like:
removing the ChannelType value. When I tried to read it back in doing:
I get:
So basically it is requiring all the fields to be there. So you need to change the
@Elementand@ElementList(inline=false)annotations to@Element(required=false)and@ElementList(inline=false, required=false)on any optional fields.