利用Java得到RSS源的内容

利用Java得到RSS源的内容

 

WebClient和WebResponse类,见文章http://blog.csdn.net/gtuu0123/archive/2009/12/06/4949087.aspx

 

IRSSParser.java

public interface IRSSParser { RSSChannel parseRSS() throws RSSException; }

 

RSSChannel.java   (对应<channel>标签)

public class RSSChannel implements Serializable { private Map<String, String> kv; private List<RSSItem> items; public RSSChannel() { kv = new HashMap<String, String>(); items = new ArrayList<RSSItem>(); } public List<RSSItem> getItems() { return items; } public void addItems(RSSItem item) { items.add(item); } public void set(String key, String value) { kv.put(key, value); } public String get(String key) { return kv.get(key); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[/n"); Set<String> keySet = kv.keySet(); Iterator<String> keyIter = keySet.iterator(); while(keyIter.hasNext()) { String key = keyIter.next(); String value = kv.get(key); sb.append(key + "=" + value + "/n"); } for (int i = 0; i < items.size(); i++) { RSSItem item = items.get(i); sb.append(item.toString() + "/n"); } sb.append("]/n"); return sb.toString(); } }

 

RSSItem.java  (对应<item>标签)

public class RSSItem implements Serializable { private Map<String, String> kv; public RSSItem() { kv = new HashMap<String, String>(); } public void set(String key, String value) { kv.put(key, value); } public String get(String key) { return kv.get(key); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[/n"); Set<String> keySet = kv.keySet(); Iterator<String> keyIter = keySet.iterator(); while(keyIter.hasNext()) { String key = keyIter.next(); String value = kv.get(key); sb.append(key + "=" + value + "/n"); } sb.append("]"); return sb.toString(); } }

 

RSSException.java

public class RSSException extends Exception { public RSSException() { super(); } public RSSException(Throwable t) { super(t); } public RSSException(String error) { super(error); } public RSSException(String error, Throwable t) { super(error, t); } }

 

DefaultRSSParser.java

public class DefaultRSSParser implements IRSSParser { private static final Log logger = LogFactory.getLog(DefaultRSSParser.class); private String url; private WebClient browser = new WebClient(); public DefaultRSSParser(String url) { this.url = url; } private Document createDOMDocument() throws RSSException { try { WebResponse rssResp = browser.doGet(url); String encoding = rssResp.getEncoding(); String rssContent = rssResp.getBodyAsString(); DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder(); InputSource is = new InputSource(new ByteArrayInputStream( rssContent.getBytes(encoding))); Document doc = docBuilder.parse(is); return doc; } catch (Exception e) { logger.error("CreateDOMDocument Error", e); throw new RSSException("CreateDOMDocument Error", e); } } private RSSChannel parseForChannel(Document doc) { RSSChannel rssChannel = new RSSChannel(); NodeList channelNodeList = doc.getElementsByTagName("channel"); Node channel = channelNodeList.item(0); NodeList childsOfChannel = channel.getChildNodes(); for (int i = 0; i < childsOfChannel.getLength(); i++) { Node childOfChannel = childsOfChannel.item(i); if (childOfChannel.getNodeType() != Node.TEXT_NODE) { String nodeName = childOfChannel.getNodeName(); if (!nodeName.equals("item")) { String name = childOfChannel.getNodeName(); String value = childOfChannel.getTextContent(); rssChannel.set(name, value); } } } return rssChannel; } private List<RSSItem> parseForItem(Document doc) { List<RSSItem> rssItems = new ArrayList<RSSItem>(); NodeList itemNodeList = doc.getElementsByTagName("item"); for (int i = 0; i < itemNodeList.getLength(); i++) { RSSItem rssItem = new RSSItem(); Node item = itemNodeList.item(i); NodeList childsOfItem = item.getChildNodes(); for (int j = 0; j < childsOfItem.getLength(); j++) { Node node = childsOfItem.item(j); if (node.getNodeType() != Node.TEXT_NODE) { String name = node.getNodeName(); String value = node.getTextContent(); rssItem.set(name, value); } } rssItems.add(rssItem); } return rssItems; } public RSSChannel parseRSS() throws RSSException { Document doc = createDOMDocument(); RSSChannel channel = parseForChannel(doc); List<RSSItem> items = parseForItem(doc); for (int i = 0; i < items.size(); i++) { channel.addItems(items.get(i)); } return channel; } }

 

RSSTest.java

public class RSSTest { public static void main(String[] args) { try { //String url = "http://blog.sina.com.cn/rss/daodaodog.xml"; String url = "http://rss.sina.com.cn/tech/rollnews.xml"; IRSSParser parser = new DefaultRSSParser(url); RSSChannel channel = parser.parseRSS(); System.out.println(channel.toString()); } catch (RSSException e) { e.printStackTrace(); } } }

你可能感兴趣的:(java,exception,rss,String,Class,encoding)