If your rss is xml format, you will need dom4j.jar
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class test {
public static void main(String[] args) throws Exception {
String rssUrl = ""; // paste url here
List<RssDocument> docList = new ArrayList<RssDocument>();
try
{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(rssUrl);
Element channel = (Element) document.getRootElement().element("channel");
for (Iterator i = channel.elementIterator("item"); i.hasNext();)
{
Element element = (Element) i.next();
String title = element.elementText("title");
String pubDate = element.elementText("pubDate");
String description = element.elementText("description");
RssDocument doc = new RssDocument(title, pubDate, description);
docList.add(doc);
}
}
catch (Exception e)
{
e.printStackTrace();
}
// do something with docList
}
public static class RssDocument {
String title;
String pubDate;
String description;
RssDocument(String title, String pubDate, String description) {
this.title = title;
this.pubDate = pubDate;
this.description = description;
}
}
}
Paste your rss url into variable "rssUrl", and run this main. You will get a list of RSS document, which contains title, published date and description.
If what you need is only the title and description of every rss item, use the following codes.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class test {
public static void main(String[] args) throws Exception {
String rssUrl = ""; // paste url here
List<String> strList = new ArrayList<String>();
try
{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(rssUrl);
Element channel = (Element) document.getRootElement().element("channel");
for (Iterator i = channel.elementIterator("item"); i.hasNext();)
{
Element element = (Element) i.next();
String title = element.elementText("title").replaceAll("\\<.*?>","");
String description = element.elementText("description").replaceAll("\\<.*?>","");
strList.add(title + " " + description);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Then strList will be the list of string, which contains title and description.
For example:
{
"title1 description1"
"title2 description2"
"title3 description3"
}