关于RSS的聚合---OPML
OPML也是最近才发现的一个东东,以前只知道解析Rss,前几天老总问我要这个东西的时候我才知道,还有OPML的存在。
什么是OPML呢?说白啦,就是把你的网站的RSS聚合在一起生成的一个OPML文件,主要也就是方便RSS文件的提交。
对OPML还没有做太深入的理解,在网上查了查,发现没有那个开源项目可以用的,有Jakarta下的一个开源项目可以使用(FeedParser),可是好像是项目还没有完成啦,在Apache里没有找到下载。还有一个Informa也可以做,不过从2004年之后就没有组织再对其进行更新和维护啦。
想来想去决定用FreeMarker做去,以模板的形式生成一个opml.ftl模板文件。模板文件如下:
<?xml version="1.0" encoding="utf-8" ?> <opml version="1.0"> <head> <title>${title}</title> <dateCreated>${pubDate}</dateCreated> <ownerName>${owner}</ownerName> <ownerEmail>${email}</ownerEmail> <description>${title}</description> <createdBy>${builder}</createdBy> <link>${link}</link> </head> <body> <outline title="${title}" text="${title}" opml="" kind="nkPath"> <#list typeList as type> <outline title="${type.title}" text="${type.title}" type="rss" xmlUrl="${buildPath}/news/rss/${type.typecode}.xml" interval="60" itemMax="400" htmlUrl="" description="${type.title}" kind="nkFeed" /> </#list> </outline> </body> </opml>
完成所需要的模板文件后,就要对模板文件进行生成啦。我使用的技术是Struts2+Spring+Hibernate+Dwr实现的,生成文件的那点是使用Dwr去实现的。具体的生成代码如下:
public String opmlBuild(String typeCode, String fileName, String owner, String email, String builder, String link, String buildPath, HttpServletRequest request, HttpServletResponse response) { String result = ""; try { response.setContentType("text/opml"); response.setCharacterEncoding("utf-8"); String ftlPath = request.getSession().getServletContext() .getRealPath("/") + "//WEB-INF//ftl//"; String path = request.getSession().getServletContext().getRealPath( "/"); // String ftlPath = ftl + "//ftl//"; config.setDirectoryForTemplateLoading(new File(ftlPath)); // 得到模板文件 Template template = config.getTemplate("opml.ftl"); // 设置编码格式 template.setEncoding("utf-8"); YxNewsType type = typeNews.getTypeByTypeCode(typeCode); List<YxNewsType> typeList = typeNews .getAllChildTypeByTypeCode(typeCode); if (typeList != null) { Map root = new HashMap(); //设置发布的OPML的标题 root.put("title", type.getTitle()); //设置OPML的发布时间 root.put("pubDate", new Date().toString()); //设置OPML的所有者 root.put("owner", owner); //设置OPML的发布者的EMail地址 root.put("email", email); //设置OPML的创建人 root.put("builder", builder); //设置OPML文件的连接地址,可以直接你网站的连接地址 root.put("link", link); //设置自己的创建Rss的连接地址 root.put("buildPath", buildPath); root.put("typeList", typeList); // 设置到生成的文件名称 String file = ""; //判断当前操作系统,如果是Windows系统的做就是反斜杠,如果是Linux系统就使用正斜杠 Properties properties = System.getProperties(); Object object = properties.get("os.name"); // System.out.println("当前操作系统:" + String.valueOf(object)); if ("Windows".equals(String.valueOf(object))) { file = path + "\\rss\\opml\\" + fileName + ".opml"; } else { file = path + "//rss//opml//" + fileName + ".opml"; } //设置要生成的文件 Writer out = new OutputStreamWriter(new FileOutputStream(file), "utf-8"); template.process(root, out); //清理写入流 out.flush(); // 关闭流文件 out.close(); result = "buildSuccess"; } else { result = "noChild"; } } catch (Exception e) { result = "error"; e.printStackTrace(); } return result; }
上面方法中的参数是通过Dwr传递过来的,下面是页面JSP文件:
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>信源管理</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/common/css/admin/style.css" type="text/css" /> <script type="text/javascript" src="${pageContext.request.contextPath}/dwr/engine.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/dwr/util.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/dwr/interface/rssBuild.js"></script> <script type="text/javascript"> var typeCode; function showBuild(title,code){ //alert(title); typeCode = code; var doc = document.getElementById("buildOpml"); document.getElementById("tableTitle").innerHTML=title; document.getElementById("opmlName").value = ""; document.getElementById("nameMessage").innerHTML = "请以英文名称命名"; document.getElementById("startBuild").value = "生成OPML"; doc.style.display = "block"; } function buildOpml(){ owner = document.getElementById("opmlOwner").value; email = document.getElementById("opmlEmail").value; builder = document.getElementById("opmlBuilder").value; url = document.getElementById("opmlUrl").value; fileName = document.getElementById("opmlName").value; prefix = document.getElementById("opmlPrefix").value; msg = document.getElementById("nameMessage"); btnBuild = document.getElementById("startBuild"); if(""!=fileName){ msg.innerHTML = "<font color='green'>文件生成中,请等待……</font>"; btnBuild.disabled = true; btnBuild.value = "文件生成中"; rssBuild.opmlBuild(typeCode,fileName,owner,email,builder,url,prefix,callBackResult); }else{ msg.innerHTML = "<font color='red'>请填写你的OPML文件名称</font>"; } } function callBackResult(result){ msg = document.getElementById("nameMessage"); btnBuild = document.getElementById("startBuild"); if(result=="buildSuccess"){ msg.innerHTML = "<font color='green'>文件已经成功生成</font>"; btnBuild.disabled = false; btnBuild.value = "生成成功"; }else if(result=="noChild"){ msg.innerHTML = "<font color='red'>此类别没有子信息,无法生成</font>"; btnBuild.disabled = false; btnBuild.value = "没有子类可供生成"; }else{ msg.innserHTML = "<font color='red'>生成出现错误,请联系管理员</font>"; btnBuild.disabled = false; btnBuild.value = "生成失败"; } } function showHideDIV(){ var doc = document.getElementById("buildOpml"); doc.style.display =(doc.style.display == "none"?"block":"none"); } </script> </head> <body> <div id="container"> <table class="tableBorder" align="center" border="0" cellpadding="2" cellspacing="1" style="width: 98%;"> <tr> <th height="25" colspan="10"> 类别名称 </th> </tr> <tr> <s:iterator value="listType"> <td class="TableRow2"> <a href="javascript:showBuild('${title }','${typecode }');" style="text-shadow: none;"> <s:property value="title" /> </a> </td> </s:iterator> </tr> </table> <div id="buildOpml" style="display:none;"> <table class="tableBorder" align="center" border="0" cellpadding="2" cellspacing="1" style="width: 98%;"> <tr> <th colspan="3"> <div id="tableTitle"></div> </th> </tr> <tr> <td class="TableRow2">请输入OPML的所有者:</td> <td class="TableRow2"> <input type="text" id="opmlOwner" value="中国分告传媒" disabled="disabled"/> </td> <td class="TableRow2"> <div id="ownerMessage">请正确填写OPML的所有者</div> </td> </tr> <tr> <td class="TableRow2">请输入OPML所有者的联系邮箱:</td> <td class="TableRow2"> <input type="text" id="opmlEmail" value="[email protected]"/> </td> <td class="TableRow2"> <div id="emailMessage">请正确填写邮箱地址</div> </td> </tr> <tr> <td class="TableRow2">请输入OPML创建者的名字:</td> <td class="TableRow2"> <input type="text" id="opmlBuilder" value="河南亿禧软件有限公司" disabled="disabled"/> </td> <td class="TableRow2"> <div id="builderMessage">请正确填写创建者</div> </td> </tr> <tr> <td class="TableRow2">请输入网站的连接URL:</td> <td class="TableRow2"> <input type="text" id="opmlUrl" value="www.fansgoo.com" disabled="disabled"/> </td> <td class="TableRow2"> <div id="urlMessage">请输入合法的网络地址</div> </td> </tr> <tr> <td class="TableRow2">请填写您生成文件前缀地址:</td> <td class="TableRow2"> <input type="text" id="opmlPrefix" value="http://www.fansgoo.com" disabled="disabled"/> </td> <td class="TableRow2">请填写您生成文件所在的位置</td> </tr> <tr> <td class="TableRow2">请输入您的生成的OPML名称:</td> <td class="TableRow2"><input type="text" id="opmlName" name="opmlName"/></td> <td class="TableRow2"><div id="nameMessage">请以英文名命名</div></td> </tr> <tr> <td class="TableRow2" colspan="3" align="center"> <input type="button" id="startBuild" value="生成OPML" onclick="buildOpml();"/> </td> </tr> </table> </div> <br /> <jsp:include page="admin_bottom.jsp" /> </div> </body> </html>
OPML是对Rss资源的整合,避免了反复提交Rss文件所带来的负担,从而提高Rss的可收录度,相信不久以后OPML和Rss可以更完美的结合,人们可以更好的使用Rss资源。
相关推荐
wanxuncpx 2020-06-01
malachuan 2020-05-17
IT前沿 2020-04-25
Vikingsoft 2020-04-23
晚安假行僧 2020-02-09
malachuan 2019-11-11
徐汇业大信息中心 2011-09-25
xiaomingqing 2015-06-29
chennai0 2015-07-22
panpanhappy 2019-07-15
iComputer 2019-09-05
川大门口贴膜小哥 2018-09-18
Vikingsoft 2018-08-18
IT前沿 2014-06-03
panpanhappy 2011-04-17
IT前沿 2013-08-27
灬扫地僧 2013-05-17