0

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?

smiley
  • 491
  • 3
  • 14
  • 36

1 Answers1

1

If you want to parse XML, use an XML parser (javax.xml.parsers.DocumentBuilder).

If the input is already in a string, just instantiate an org.xml.sax.InputSource as a wrapper for the String and pass that to DocumentBuilder#parse(InputSource is). Assuming it's valid XML all the hard work is done and you can cherry-pick the data you want.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Jim, I have posted another question here - http://stackoverflow.com/questions/18583487/how-to-set-nodevalues-to-bean-and-add-to-arraylist-of-beans Can you please help me make corrections in the code.. Looks like I almost got what I wanted, only a small problem.. some type mismatch I believe. It would be great help if you can check it and let me know your thoughts! Thanks for all your help! – smiley Sep 03 '13 at 03:09