EXT的例子里面包含了一个FeedView的例子,是用EXT做一个Feed订阅器。
?
但EXT的例子是用PHP写的,我比较喜欢用Java,所以用Java对它进行了改造。
?
首先,做一个Feed订阅器必须要做一个订阅器。
?
feedproxy.jsp的页面如下:
?
<%@ page language="java" import="java.util.*,java.net.*,java.io.*,org.dom4j.*,org.json.simple.*" pageEncoding="UTF-8"%><% String feed = request.getParameter("feed"); String coding = request.getParameter("coding"); if(coding==null || coding.trim().length()==0){ coding = "UTF-8"; } URL url = new URL(feed); // 获取http连接对象 HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); urlConnection.connect(); /** * 通过解析meta得到网页编码 */ // 获取网页源码(英文字符和数字不会乱码,所以可以得到正确<meta/>区域) StringBuffer sb = new StringBuffer(); String line; try { BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream(),coding)); while ((line = in.readLine()) != null) { sb.append(line); } in.close(); } catch (Exception e) { // Report any errors that arise System.err.println(e); System.err .println("Usage: java HttpClient <URL> [<filename>]"); } String xmlcode = sb.toString(); xmlcode = xmlcode.replaceAll("<content:encoded>", "<content>"); xmlcode = xmlcode.replaceAll("</content:encoded>", "</content>"); xmlcode = xmlcode.replaceAll("</dc:creator>", "</author>"); xmlcode = xmlcode.replaceAll("<dc:creator", "<author"); Document document = DocumentHelper.parseText(xmlcode); Element root = document.getRootElement(); Document docFormat = DocumentHelper.createDocument(); Element rootFormat = docFormat.addElement("channel");// 创建根节点 List nodes = root.elements("channel"); Element chanel = null; for (Iterator it = nodes.iterator(); it.hasNext();) { chanel = (Element) it.next(); } List nodes2 = chanel.selectNodes("item"); JSONObject rootJson = new JSONObject(); JSONArray arr = new JSONArray(); rootJson.put("root",arr); for (Iterator it = nodes2.iterator(); it.hasNext();) { Element item = (Element) it.next(); Element formatItem = rootFormat.addElement("item"); Element title = formatItem.addElement("title"); title.setText(item.element("title").getText()); JSONObject obj = new JSONObject(); obj.put("title",item.element("title").getText()); obj.put("author",item.element("author").getText()); obj.put("pubDate",item.element("pubDate").getText()); obj.put("link",item.element("link").getText()); obj.put("description",item.element("description").getText()); arr.add(obj); } System.out.println(rootJson.toString()); out.print(rootJson.toString()); %>
?这是我写了一个JSP页面,来代码原本的PHP页面,用Dom4j来解析XML,用SimpleJSON来生成JSON字符串。
?
在例子的文件夹里面找到FeedGrid.js文件,把Store改成JSONStore。
?
this.store = new Ext.data.JsonStore({ // store configs autoDestroy: true, url: 'feed-proxy.jsp', storeId: 'myStore', // reader configs root: 'root', idProperty: 'title', fields: ['title','author',{name:'pubDate', type:'date'}, 'link', 'description', 'content'], autoLoad : true });
?
例子里面带有三个Feed地址,其实只有第二个可以用,其他两个都已经不能用了。
?
// add some default feeds feeds.addFeed({ url:'http://bbs.maxthon.cn/rss.php?fid=56&auth=0', text: '傲游论坛' }, false, true); feeds.addFeed({ url:'http://www.sencha.com/forum/external.php?type=RSS2', text: 'ExtJS.com Forums' }, true); feeds.addFeed({ url:'http://news.qq.com/newsgn/rss_newsgn.xml', text: '腾讯新闻', coding:'GBK' }, true);
?这里,我把它们改成这样,哈哈,可以订阅腾讯的新闻了,呵呵。