Java代碼
/**
* 根據(jù)鏈接地址得到
數(shù)據(jù) * @param url RSS形式的
xml文件 * @throws IllegalArgumentException
* @throws FeedException
*/
public void parseXml(URL url) throws IllegalArgumentException, FeedException {
try {
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = null;
URLConnection conn;
conn = url.openConnection();
String content_encoding = conn.getHeaderField("Content-Encoding");
if (content_encoding != null && content_encoding.contains("gzip")) {
System.out.println("conent encoding is gzip");
GZIPInputStream gzin = new GZIPInputStream(conn
.getInputStream());
feed = input.build(new XmlReader(gzin));
} else {
feed = input.build(new XmlReader(conn.getInputStream()));
}
List entries = feed.getEntries();//得到所有的標(biāo)題<title></title>
for(int i=0; i < entries.size(); i++) {
SyndEntry entry = (SyndEntry)entries.get(i);
System.out.println(entry.getTitle());
}
System.out.println("feed size:" + feed.getEntries().size());
} catch (IOException e) {
e.printStackTrace();
}
}
public void createXml() throws Exception {
/* 根據(jù)Channel
源碼提供的英文,Channel
對象有兩個構(gòu)造器,一個默認(rèn)的無參構(gòu)造器用于clone對象,一個是有參的
* 我們自己指定的必須使用有
參數(shù)的(因?yàn)槲覀冃枰S可證),指構(gòu)造方法必須要創(chuàng)建一個type(版本),這個type不能隨便寫,必須要以rss_開頭的版本號
* Licensed under the Apache License, Version 2.0 (the "License");
* 因?yàn)楫?dāng)前版本是2.0,所以就是rss_2.0,必須是rss_2.0否則會拋異常,該源碼中寫的已經(jīng)很明白。
*/
Channel channel = new Channel("rss_2.0");
channel.setTitle("channel標(biāo)題");//網(wǎng)站標(biāo)題
channel.setDescription("channel的描述");//網(wǎng)站描述
channel.setLink("www.shlll.net");//網(wǎng)站主頁鏈接
channel.setEncoding("utf-8");//RSS文件編碼
channel.setLanguage("zh-cn");//RSS使用的語言
channel.setTtl(5);//time to live的簡寫,在刷新前當(dāng)前RSS在緩存中可以保存多長時間(分鐘)
channel.setCopyright("版權(quán)聲明");//版權(quán)聲明
channel.setPubDate(new Date());//RSS發(fā)布時間
List<Item> items = new ArrayList<Item>();//這個list對應(yīng)rss中的item列表
Item item = new Item();//新建Item對象,對應(yīng)rss中的<item></item>
item.setAuthor("hxliu");//對應(yīng)<item>中的<author></author>
item.setTitle("新聞標(biāo)題");//對應(yīng)<item>中的<title></title>
item.setGuid(new Guid());//GUID=Globally Unique Identifier 為當(dāng)前新聞指定一個全球唯一標(biāo)示,這個不是必須的
item.setPubDate(new Date());//這個<item>對應(yīng)的發(fā)布時間
item.setComments("注釋");//代表<item>節(jié)點(diǎn)中的<comments></comments>
//新建一個Description,它是Item的描述部分
Description description = new Description();
description.setValue("新聞主題");//<description>中的內(nèi)容
item.setDescription(description);//添加到item節(jié)點(diǎn)中
items.add(item);//代表一個段落<item></item>,
channel.setItems(items);
//用WireFeedOutput對象輸出rss文本
WireFeedOutput out = new WireFeedOutput();
try {
System.out.println(out.outputString(channel));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
}
}
/**
* 根據(jù)鏈接地址得到數(shù)據(jù)
* @param url RSS形式的xml文件
* @throws IllegalArgumentException
* @throws FeedException
*/
public void parseXml(URL url) throws IllegalArgumentException, FeedException {
try {
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = null;
URLConnection conn;
conn = url.openConnection();
String content_encoding = conn.getHeaderField("Content-Encoding");
if (content_encoding != null && content_encoding.contains("gzip")) {
System.out.println("conent encoding is gzip");
GZIPInputStream gzin = new GZIPInputStream(conn
.getInputStream());
feed = input.build(new XmlReader(gzin));
} else {
feed = input.build(new XmlReader(conn.getInputStream()));
}
List entries = feed.getEntries();//得到所有的標(biāo)題<title></title>
for(int i=0; i < entries.size(); i++) {
SyndEntry entry = (SyndEntry)entries.get(i);
System.out.println(entry.getTitle());
}
System.out.println("feed size:" + feed.getEntries().size());
} catch (IOException e) {
e.printStackTrace();
}
}
public void createXml() throws Exception {
/* 根據(jù)Channel源碼提供的英文,Channel對象有兩個構(gòu)造器,一個默認(rèn)的無參構(gòu)造器用于clone對象,一個是有參的
* 我們自己指定的必須使用有參數(shù)的(因?yàn)槲覀冃枰S可證),指構(gòu)造方法必須要創(chuàng)建一個type(版本),這個type不能隨便寫,必須要以rss_開頭的版本號
* Licensed under the Apache License, Version 2.0 (the "License");
* 因?yàn)楫?dāng)前版本是2.0,所以就是rss_2.0,必須是rss_2.0否則會拋異常,該源碼中寫的已經(jīng)很明白。
*/
Channel channel = new Channel("rss_2.0");
channel.setTitle("channel標(biāo)題");//網(wǎng)站標(biāo)題
channel.setDescription("channel的描述");//網(wǎng)站描述
channel.setLink("www.shlll.net");//網(wǎng)站主頁鏈接
channel.setEncoding("utf-8");//RSS文件編碼
channel.setLanguage("zh-cn");//RSS使用的語言
channel.setTtl(5);//time to live的簡寫,在刷新前當(dāng)前RSS在緩存中可以保存多長時間(分鐘)
channel.setCopyright("版權(quán)聲明");//版權(quán)聲明
channel.setPubDate(new Date());//RSS發(fā)布時間
List<Item> items = new ArrayList<Item>();//這個list對應(yīng)rss中的item列表
Item item = new Item();//新建Item對象,對應(yīng)rss中的<item></item>
item.setAuthor("hxliu");//對應(yīng)<item>中的<author></author>
item.setTitle("新聞標(biāo)題");//對應(yīng)<item>中的<title></title>
item.setGuid(new Guid());//GUID=Globally Unique Identifier 為當(dāng)前新聞指定一個全球唯一標(biāo)示,這個不是必須的
item.setPubDate(new Date());//這個<item>對應(yīng)的發(fā)布時間
item.setComments("注釋");//代表<item>節(jié)點(diǎn)中的<comments></comments>
//新建一個Description,它是Item的描述部分
Description description = new Description();
description.setValue("新聞主題");//<description>中的內(nèi)容
item.setDescription(description);//添加到item節(jié)點(diǎn)中
items.add(item);//代表一個段落<item></item>,
channel.setItems(items);
//用WireFeedOutput對象輸出rss文本
WireFeedOutput out = new WireFeedOutput();
try {
System.out.println(out.outputString(channel));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
}
}