我的代码中有从数据库中提取的 RSS 字符串。我需要解析每个值并存储在 Bean 的 ArrayList 中。例如,title、link 和 description 中的所有值都应该存储在 arraylist 中。
这是我的豆类
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;
}
}
主程序是这样的——
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
}
}
我无法得到结果,看起来我的逻辑完全错误......我看到程序终止了。有人可以更正上面的代码并指导我得到结果吗?