I am having RSS string in my code which is pulled from the database. I need to parse each of the values and store in a ArrayList of Beans. For example, all values in title, link and description should be stored in the arraylist.
This is my Bean class
public class RssBean {
String title;
String link;
String description;
public RssBean(String title,String link,String description)
{
this.title=title;
this.link = link;
this.description = description;
}
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 getDesc(){
return description;
}
public void setDesc(String description){
this.description=description;
}
}
Main program goes this way -
import java.util.ArrayList;
public class RssToList {
public static void main(String args[])
{ ArrayList<RssBean> list = new ArrayList<RssBean>();
RssBean bean = new RssBean(null, null, null);
String rss = "<rss version="+"\"2.0\"\">" +
"<channel>"+
"<title>W3Schools Home Page</title>"+
"<link>http://www.w3schools.com</link>"+
"<description>Free web building tutorials</description>"+
"<item> "+
"<title>RSS Tutorial</title>"+
"<link>http://www.w3schools.com/rss</link>"+
"<description>New RSS tutorial on W3Schools</description>"+
"</item>"+
"</channel>" +
"</rss>";
for(int i=0;i<rss.length();i++)
{
while(rss.startsWith("<item>") && rss.endsWith("</item>"))
{
// bean.setTitle(rss.substring(rss.indexOf("<title>"),rss.lastIndexOf("</title>")));
// bean.setLink(rss.substring(rss.indexOf("<link>"),rss.lastIndexOf("</link>")));
// bean.setDesc(rss.substring(rss.indexOf("<description>"),rss.lastIndexOf("</description>")));
String title = StringUtils.substringBetween(rss, "<title>", "</title>");
String link = StringUtils.substringBetween(rss, "<link>", "</link>");
String description = StringUtils.substringBetween(rss, "<description>", "</description>");
bean.setTitle(title);
bean.setLink(link);
bean.setDesc(description);
System.out.println(bean);
} //end if
//add the bean to list
list.add(bean);
}//end for
}
}
I am not able to get the results, looks like my logic is completely wrong... I see program terminated. Can someone correct the above code and guide me to the result?