这个只是我试验google xslt的功能的一个小demo,很简单,另外使用到了XHConn。

HTML:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE>Google Ajaxslt rss reader demo</TITLE>
<script type=”text/javascript” src=”XHConn.js”></script>
<script src=”misc.js” type=”text/javascript”></script>
<script src=”dom.js” type=”text/javascript”></script>
<script src=”xpath.js” type=”text/javascript”></script>
<script src=”xslt.js” type=”text/javascript”></script>
<script type=”text/javascript”>
var _rssxslt=”<xsl:stylesheet><xsl:template match=\”/\”>”;
_rssxslt+=”<xsl:apply-templates select=\”rss/channel/item\”/>”;
_rssxslt+=”</xsl:template>”;
_rssxslt+=”<xsl:template match=\”rss/channel/item\”>”;
_rssxslt+=”<xsl:element name=\”a\”><xsl:attribute name=\”href\”><xsl:value-of select=\”link\”/></xsl:attribute>”;
_rssxslt+=”<xsl:value-of select=\”title\”/>”;
_rssxslt+=”</xsl:element>”;
_rssxslt+=”<br/>”;
_rssxslt+=”<xsl:value-of select=\”description\”/>”;
_rssxslt+=”<br/>”;
_rssxslt+=”</xsl:template>”;
_rssxslt+=”</xsl:stylesheet>”;
var myConn;
  function read()  {
    el('htmldisplay').innerHTML = “Reading”;
    myConn = new XHConn();
   
    if (!myConn) alert(“XMLHTTP not available. Try a newer/better browser.”);
   
    var fnWhenDone = function (oXML) {
    //logging__=false;
    var html = xsltProcess(xmlParse(oXML.responseText), xmlParse(_rssxslt));
    el('htmldisplay').innerHTML = html;
   };
    myConn.connect(“rssReader”, “POST”, “param=”+el('url').value, fnWhenDone);
  }
 
</script>
</HEAD>

<BODY>
<h1>Google Ajaxslt rss reader demo</h1>
<div id=”sample”>
Sample:
<ul>
<li>http://www.javaresearch.org/rss/RSS2_article.xml
<li>http://www.nytimes.com/services/xml/rss/nyt/AsiaPacific.xml
</ul>
</div>
Log:
<div id=”log”></div>
<br />
<br />

RSS feed: <input type=”text” id='url' size=80 value='http://www.javaresearch.org/rss/RSS2_article.xml'><INPUT id='readbutton' TYPE=”button” value=”Read” onclick=”read()” />
<div id=”htmldisplay”></div>
</BODY>
</HTML>

AJAX据说可以读取外部的数据,但是我试验了一下好像不行,XMLHttpRequest打开的链接不能是以http开头的外部链接,不得以写了一个servlet:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RssReaderServlet extends HttpServlet {
  public void init() throws ServletException {
  }

  public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType(“text/xml”);
    response.setHeader(“Cache-Control”, “no-cache”);
    OutputStream pw = response.getOutputStream();
    URL url = new URL(request.getParameter(“param”));
    URLConnection connection = url.openConnection();
    BufferedInputStream reader = new BufferedInputStream(connection
        .getInputStream());
    int length = 0;
    byte[] buffer = new byte[1024];
    length = reader.read(buffer);
    while (length != -1) {
      pw.write(buffer, 0, length);
      length = reader.read(buffer);
    }
    pw.flush();
  }

}

web.xml里面的片段
  <servlet>
    <servlet-class>org.jr.ajax.RssReaderServlet</servlet-class>
    <servlet-name>rssReader</servlet-name>
  </servlet>
  <servlet-mapping>
    <servlet-name>rssReader</servlet-name>
    <url-pattern>/rssReader</url-pattern>
  </servlet-mapping>

(Visited 119 times, 1 visits today)