import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.zip.GZIPInputStream;
import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Guid; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.WireFeedOutput; import com.sun.syndication.io.XmlReader;
//http://rss.webofknowledge.com/rss?e=4f1b64b6b221ea05&c=8c6909b93bf3eb38a2066a826b61a412 public class Test {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> parseXml(new </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> URL("</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://rss.webofknowledge.com/rss?e=4f1b64b6b221ea05</span><span style="color: rgba(0, 128, 0, 1)">&c=8c6909b93bf3eb38a2066a826b61a412"));</span> parseXml(<span style="color: rgba(0, 0, 255, 1)">new</span> URL("http://127.0.0.1:8080/xd/rss?q=java&t=atom&h=50&s=50"<span style="color: rgba(0, 0, 0, 1)">)); String xml </span>=<span style="color: rgba(0, 0, 0, 1)"> createXml(); parseString(xml); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>
e.printStackTrace();
} } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> parseString(String xml) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { SyndFeedInput input </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SyndFeedInput(); SyndFeed feed </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; ByteArrayInputStream inputStream </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ByteArrayInputStream(xml.getBytes("UTF-8"<span style="color: rgba(0, 0, 0, 1)">)); feed </span>= input.build(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlReader(inputStream)); List entries </span>= feed.getEntries();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 得到所有的标题<title></title></span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < entries.size(); i++<span style="color: rgba(0, 0, 0, 1)">) { SyndEntry entry </span>=<span style="color: rgba(0, 0, 0, 1)"> (SyndEntry) entries.get(i); System.out.println(entry.getTitle()); } System.out.println(</span>"feed size:" +<span style="color: rgba(0, 0, 0, 1)"> feed.getEntries().size()); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 根据链接地址得到数据 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> parseXml(URL url) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IllegalArgumentException, FeedException { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { SyndFeedInput input </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SyndFeedInput(); SyndFeed feed </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; URLConnection conn; conn </span>=<span style="color: rgba(0, 0, 0, 1)"> url.openConnection(); String content_encoding </span>= conn.getHeaderField("Content-Encoding"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (content_encoding != <span style="color: rgba(0, 0, 255, 1)">null</span> && content_encoding.contains("gzip"<span style="color: rgba(0, 0, 0, 1)">)) { System.out.println(</span>"conent encoding is gzip"<span style="color: rgba(0, 0, 0, 1)">); GZIPInputStream gzin </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> GZIPInputStream( conn.getInputStream()); feed </span>= input.build(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlReader(gzin)); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { feed </span>= input.build(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlReader(conn.getInputStream())); } List entries </span>= feed.getEntries();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 得到所有的标题<title></title></span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < entries.size(); i++<span style="color: rgba(0, 0, 0, 1)">) { SyndEntry entry </span>=<span style="color: rgba(0, 0, 0, 1)"> (SyndEntry) entries.get(i); System.out.println(entry.getTitle()); } System.out.println(</span>"feed size:" +<span style="color: rgba(0, 0, 0, 1)"> feed.getEntries().size()); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String createXml() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的 * 我们自己指定的必须使用有参数的(因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号 * Licensed under the Apache License, Version 2.0 (the "License"); * 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> Channel channel </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Channel("rss_2.0"<span style="color: rgba(0, 0, 0, 1)">); channel.setTitle(</span>"channel标题");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">网站标题</span> channel.setDescription("channel的描述");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">网站描述</span> channel.setLink("www.shlll.net");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">网站主页链接</span> channel.setEncoding("utf-8");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">RSS文件编码</span> channel.setLanguage("zh-cn");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">RSS使用的语言</span> channel.setTtl(5);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)</span> channel.setCopyright("版权声明");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">版权声明</span> channel.setPubDate(<span style="color: rgba(0, 0, 255, 1)">new</span> Date());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">RSS发布时间</span> List<Item> items = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<Item>();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这个list对应rss中的item列表</span> Item item = <span style="color: rgba(0, 0, 255, 1)">new</span> Item();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">新建Item对象,对应rss中的<item></item></span> item.setAuthor("hxliu");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对应<item>中的<author></author></span> item.setTitle("新闻标题");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对应<item>中的<title></title></span> item.setGuid(<span style="color: rgba(0, 0, 255, 1)">new</span> Guid());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">GUID=Globally Unique Identifier 为当前新闻指定一个全球唯一标示,这个不是必须的</span> item.setPubDate(<span style="color: rgba(0, 0, 255, 1)">new</span> Date());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这个<item>对应的发布时间</span> item.setComments("注释");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">代表<item>节点中的<comments></comments> </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">新建一个Description,它是Item的描述部分</span> Description description = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Description(); description.setValue(</span>"新闻主题");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"><description>中的内容</span> item.setDescription(description);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加到item节点中</span> items.add(item);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">代表一个段落<item></item>,</span>
channel.setItems(items);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用WireFeedOutput对象输出rss文本</span> WireFeedOutput out = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WireFeedOutput(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { String xml </span>=<span style="color: rgba(0, 0, 0, 1)"> out.outputString(channel); System.out.println(xml); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> xml; } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IllegalArgumentException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (FeedException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/14713.html