IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  XML  >

技巧: 从远程 RDF 文件获取标题

检索文件、转换文件然后显示结果

developerWorks
文档选项

未显示需要 JavaScript 的文档选项


级别: 初级

Nicholas Chase (nicholas@nicholaschase.com), 总裁, Chase and Chase, Inc.

2002 年 4 月 01 日

在这篇技巧文章里,Nick 向您演示如何检索来自 RDF 和 RSS 文件的联合内容并将其转换成站点的标题。

Web 发布的一个热门趋势是联合放置在其它站点上的内容。通常这一点通过使用 RDF(资源定义框架)使标题可用来完成。RDF 是一种描述内容的方式,通常与 RSS(丰富站点摘要(Rich Site Summary)或 RDF 站点摘要(RDF Site Summary))一起使用,RSS 是一种标准 XML 结构,该结构定义了一个信息“通道”以及其中所含的项目。可以从联合的站点检索 RSS 信息,然后将其转换成 HTML 以显示带有回到原始内容的链接的标题。

这篇技巧文章中描述的过程包括三个步骤:

  • 检索文件
  • 转换文件
  • 显示结果

这篇技巧文章记录了 JSP 页面的创建过程:使用 Java bean 检索并转换 RSS 信息,然后将新转换的信息合并至页面。但是,这些概念适用于一切 Web 环境。

源文件

标题的源文件,也称为 RDF 文件或 RSS 文件,可能以几种相关的格式出现。有些站点使用简单 RSS 格式:


简单 RSS 文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
 "http://my.netscape.com/publish/formats/rss-0.91.dtd">
<rss version="0.91">
  <channel>
    <title>The Vanguard Science Fiction Report</title>
    <link>http://www.vanguardreport.com</link>
    <description>You give us 22 millirotations, we'll give you the      
                 universe!</description>
    <language>en-us</language>
  </channel>
  <image>
    <title>Vanguard Report Logo</title>
    <url>http://www.vanguardreport.com/images/topics/generic.jpg</url>
    <link>http://www.vanguardreport.com</link>
  </image>
  <item>
    <title>SF Weekend Box Office (3/22/02-3/24/02)</title>
    <link>http://vanguardreport.com/modules.php?id=1</link>
  </item>
  <item>
    <title>Got a script? This could be your chance...</title>
    <link>http://vanguardreport.com/modules.php?id=2</link>
  </item>
  <item>
    <title>Antigravity in a bottle?</title>
    <link>http://vanguardreport.com/modules.php?id=3</link>
  </item>
</rss>

上面的清单包含所有标题和内容信息,但不包含 RDF 信息。RSS 文件也可以采用包含 RDF 的较长格式:


带有 RDF 信息的 RSS 文件
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
         xmlns:dc="http://purl.org/dc/elements/1.1/" 
         xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" >
  <channel>
    <title>The Vanguard Science Fiction Report</title>
    <link>http://www.vanguardreport.com</link>
    <description>You give us 22 millirotations, we'll give you the      
                 universe!</description>
    <dc:language>en-us</dc:language> 
    <dc:rights>Copyright 2002, The Vanguard Report</dc:rights> 
    <dc:date>2002-03-27T14:13:03+00:00</dc:date> 
    <dc:publisher>Nicholas Chase</dc:publisher> 
    <dc:creator>newsline@vanguardreport.com</dc:creator> 
    <dc:subject>Entertainment</dc:subject> 
    <syn:updatePeriod>daily</syn:updatePeriod> 
    <syn:updateFrequency>2</syn:updateFrequency> 
    <syn:updateBase>1970-01-01T00:00+00:00</syn:updateBase> 
    <image rdf:resource="http://www.vanguardreport.com/images/topics/generic.jpg"/> 
  </channel>
  <image>
    <title>Vanguard Report Logo</title>
    <url>http://www.vanguardreport.com/images/topics/generic.jpg</url>
    <link>http://www.vanguardreport.com</link>
  </image>
  <items> 
    <rdf:Seq> 
      <rdf:li rdf:resource="http://vanguardreport.com/modules.php?id=1"/> 
      <rdf:li rdf:resource="http://vanguardreport.com/modules.php?id=2"/> 
      <rdf:li rdf:resource="http://vanguardreport.com/modules.php?id=3"/> 
    </rdf:Seq> 
  </items>
  <item rdf:about="http://vanguardreport.com/modules.php?id=1">
    <title>SF Weekend Box Office (3/22/02-3/24/02)</title>
    <link>http://vanguardreport.com/modules.php?id=1</link>
    <dc:creator>NC</dc:creator> 
    <dc:subject>news</dc:subject> 
    <dc:date>2002-03-10T16:22:41+00:00</dc:date> 
  </item>
  <item rdf:about="http://vanguardreport.com/modules.php?id=2">
    <title>Got a script? This could be your chance...</title>
    <link>http://vanguardreport.com/modules.php?id=2</link>
    <dc:creator>NC</dc:creator> 
    <dc:subject>news</dc:subject> 
    <dc:date>2002-03-10T16:22:41+00:00</dc:date> 
  </item>
  <item rdf:about="http://vanguardreport.com/modules.php?id=3">
    <title>Antigravity in a bottle?</title>
    <link>http://vanguardreport.com/modules.php?id=3</link>
    <dc:creator>NC</dc:creator> 
    <dc:subject>news</dc:subject> 
    <dc:date>2002-03-10T16:22:41+00:00</dc:date> 
  </item>
</rdf:RDF>

RDF 信息(在 rdf: namespace 中)― 包括 Dublin Core 信息(在 dc: namespace 中)― 仅描述了内容。可以在最终显示中使用它,但并不是所有站点都包含来自 RDF 名称空间的信息,因此这篇技巧文章涵盖一个处理实际内容的更一般的示例。





回页首


样式表

我们的最终目标是生成 HTML 文本,该文本以有条理的方式(例如链接列表)显示信息,这些信息包含在另一个信息页的主体之内。实际的 HTML 输出与以下相似:


目标 HTML
<img src="http://www.vanguardreport.com/images/topics/generic.jpg" 
     style="float:left; padding: 10px;">
<h2>The Vanguard Science Fiction Report</h2>
<h3>You give us 22 millirotations, we'll give you the universe!</h3>
<ul>
  <li>
    <a href="http://vanguardreport.com/modules.php?id=1">
      SF Weekend Box Office (3/22/02-3/24/02)
    </a>
  </li>
  <li>
    <a href="http://vanguardreport.com/modules.php?id=2">
      Got a script? This could be your chance...
    </a>
  </li>
  <li>
    <a href="http://vanguardreport.com/modules.php?id=3">
      Antigravity in a bottle?
    </a>
  </li>
</ul>

实现这一点需要通过使用 XSLT 样式表转换 XML:


XSLT 样式表
<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
  <xsl:apply-templates select="//channel"/>
  <ul>
    <xsl:apply-templates select="//item"/>
  </ul>
</xsl:template>
<xsl:template match="channel">
    
<xsl:apply-templates select="../image"/>
 <h2><xsl:value-of select="title"/></h2>
 <h3><xsl:value-of select="description"/></h3>
</xsl:template>
<xsl:template match="item">
  <li>
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
      <xsl:value-of select="title" />
    </xsl:element>
  </li>
</xsl:template>
<xsl:template match="image">
  <xsl:element name="img">
    <xsl:attribute name="src"><xsl:value-of select="url"/></xsl:attribute>
    <xsl:attribute name="style">float:left; padding: 10px;</xsl:attribute>
  </xsl:element>
</xsl:template>
<xsl:template match="language">
</xsl:template>
</xsl:stylesheet>

样式表的实际格式取决于您。这个版本只是输出联合者的名称和描述,以及可选的图像,还有作为指回标题原始文章的分隔符列表的标题。





回页首


创建 JSP

有许多创建动态 Web 内容的方法。这篇技巧文章记录了 JSP 页面的创建过程:使用 Java bean 检索并转换 RSS 信息,然后将新转换的信息合并至页面。相同的基本原理可以用于 servlet 或任何其它动态页面创建平台。

JSP 本身十分简单:


JSP
<%@ page import="syndication.RSSFileBean;" %>
<jsp:useBean id="rssFile" class="syndication.RSSFileBean" 
                          scope="request"/>
<html>
<head><title>Sample RSS Include</title></head>
<% rssFile.setRSSFile(
           "http://www.vanguardreport.com/phpnuke/backend.xml"
                      ); %>
<body>
<center>
<h2>Sample RSS Include Page</h2>
</center>
<table align="right" width="50%" style="border: 1px solid black">
<tr><td><jsp:include page="headlines.html" flush="true"/></td></tr>
</table>
<p>This page typically has other information, which represents the main site copy.</p>
</body>
</html>

首先导入 bean,然后实例化该 bean 并将其命名为 rssFileRequestsetRSSFile 方法创建静态 HTML 文件 headlines.html 。如同您将在下一节看到的一样,这允许 JSP 使用 include 伪指令在页面内适当的位置包含该静态文件。





回页首


检索并解析 RSS 文件

实际上是 RSSFileBean 执行检索 RSS 内容并将其转换成静态 headlines.html 文件的工作:


Java Bean
package syndication;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
public class RSSFileBean{
    private String _rssfile;
   public void setRSSFile(String fileName){
    	
      try {
         String XMLFileName = fileName;
         String XSLSheetName = "headlines.xsl";
         String outputURL = "headlines.html";
         StreamSource style = new StreamSource(XSLSheetName);
         StreamSource source = new StreamSource(XMLFileName);
         StreamResult result = new StreamResult(new 
                                      FileOutputStream(outputURL));
         TransformerFactory transFactory = 
                                   TransformerFactory.newInstance();
         Transformer transformer = transFactory.newTransformer(style);   
         transformer.transform(source, result);
      } catch (Exception e) {
      	
      	      _rssfile = e.getMessage();
      	
      }
      
   }
    public String getRSSFile(){
        return _rssfile;
    }
}

setRSSFile 方法就象任何其它转换方法一样。它接收有关源文件(这里是 fileName )、XSLT 样式表以及信息将输出到的文件等信息。JSP 调用该方法时,文件名称和 URL 用于创建 StreamSourceStreamResult 对象,随后在转换中可以使用这两个对象。bean 然后使用 headlines.xsl 样式表创建转换程序,于是转换开始,并将结果输出由 JSP 包含的静态文件。

注:输入文件路径时,为找到所有文件,可能要使用应用程序服务器的绝对 URI。





回页首


下一步

这完成了从 RSS 文件检索标题的基本任务。但是,在实际使用这一解决方案之前,您必须修改 bean 以使它每隔特定的时间(例如 30 分钟)才检索数据以免使服务器负荷过重。

您也应该“预处理”输入的 RSS 文件,用“&”替换所有作为包含的 URI 一部分的 &,这样解析器才不会将它们误认为是不完整的实体。转换过程将会撤销这一替换,将 URI 返回到它们的原始状态。



参考资料

  • 您可以参阅本文在 developerWorks 全球站点上的 英文原文.

  • 请检查 RDF 规范

  • W3C 托管着用于检查联合文件的 RDF Validator

  • 在 developerWorks XML 专区上找到更多 XML 参考资料。

  • IBM WebSphere Studio Site Developer是一个易于使用的集成开发环境,用于构建、测试并部署 Java Server Pages、servlet 和与 XML 有关的应用程序及网站。


关于作者

Nicholas Chase 一直参与为包括 Lucent Technologies、Sun Microsystems、Oracle 和 Tampa Bay Buccaneers 在内的多家公司的网站开发。Nick 曾是一名高中物理教师、低级放射性废物设施管理员、在线科幻小说杂志编辑、多媒体工程师和 Oracle 讲师。最近,他是佛罗里达州 Clearwater 的 Site Dynamics Interactive Communications 的首席技术官。他写了三本有关 Web 开发的书,包括 Java and XML From Scratch(Que)。他乐于倾听读者的意见,可以通过 nicholas@nicholaschase.com 与他联系。




对本文的评价










回页首


IBM 公司保留在 developerWorks 网站上发表的内容的著作权。未经IBM公司或原始作者的书面明确许可,请勿转载。如果您希望转载,请通过 提交转载请求表单 联系我们的编辑团队。
    关于 IBM 隐私条约 联系 IBM 使用条款