谁能介绍一下新浪点点通阅读器中是如何使用Rss技术?100分。有详细的文档最好

usaspy 2005-03-25 11:10:12
准备做一个B/S结构的阅读器,类似于新浪点点通阅读器,看了一些资料知道需要采用RSS,但是不知道具体的实现原理,通常做类似于此的东西直接从数据库中读就是了,
RSS技术到底是怎么实现的?
...全文
229 点赞 收藏 9
写回复
9 条回复
fsqsd 2005年07月16日
mark
回复 点赞
小小草 2005年03月31日
public void convertChannelToHtml(XmlPullParser parser, XmlSerializer writer)
throws IOException, XmlPullParserException
{
// <!ELEMENT channel (title | description | link | language | item+ | rating? | image? | textinput? | copyright? | pubDate? | lastBuildDate? | docs? | managingEditor? | webMaster? | skipHours? | skipDays?)*>
boolean seenBody = false; //assumption that title is before items ...
while (parser.nextTag() != XmlPullParser.END_TAG) { // this guranteed by well formednes of XML && parser.getName().equals("channel"))) {
// if (parser.getEventType() == XmlPullParser.START_TAG) { //guranteed by nextTag
// <!ELEMENT title (#PCDATA)>
if(parser.getName().equals("title") && !seenBody) {
writer.startTag(null,"head");
writer.startTag(null,"title").text(parser.nextText()).endTag(null,"title");
writer.endTag(null,"head");
} else if(parser.getName().equals("item")) {
if(!seenBody) {
writer.startTag(null, "body");
seenBody = true;
}
convertItemToHtml(parser, writer);
} else {
// skip any element content including sub elements...
int level = 1;
while (level > 0) {
switch(parser.next()) {
case XmlPullParser.START_TAG: ++level; break;
case XmlPullParser.END_TAG: --level; break;
}
}
}
}
if(seenBody) writer.endTag(null, "body");
}

public void convertItemToHtml(XmlPullParser parser, XmlSerializer writer)
throws IOException, XmlPullParserException
{
writer.startTag(null, "p");
//<!ELEMENT item (title | link | description)*>
String title = null, link = null, description = null;
while (parser.nextTag() != XmlPullParser.END_TAG) {
if (parser.getName().equals("title")) {
title = parser.nextText();
} else if (parser.getName().equals("link")) {
link = parser.nextText();
} else if (parser.getName().equals("description")) {
description = parser.nextText();
}
}

//HashMap attributes = new HashMap(1);
//if(link != null) attributes.put("href", link);
//writer.beginElement("a",attributes);
writer.startTag(null, "a");
writer.attribute(null, "href", link);
if(title != null) {
writer.text(title);
} else {
writer.text(link);
}
writer.endTag(null,"a");

//writer.writeEmptyElement("br");
writer.startTag(null, "br").endTag(null, "br");

if(description != null) writer.text(description);

writer.endTag(null, "p"); // end the "p" element
}
}

回复 点赞
小小草 2005年03月31日
// modified by Aleksander Slominski
// based on http://www.xml.com/pub/a/2002/05/22/parsing.html?page=2

import java.io.*;
import java.net.*;

import org.xmlpull.v1.*;

public class RSSReader {

public static void main(String [] args)
{

// create an instance of RSSReader
RSSReader rssreader = new RSSReader();

try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
String url = args[0];
InputStreamReader stream = new InputStreamReader(
new URL(url).openStream());
parser.setInput(stream);
XmlSerializer writer = factory.newSerializer();
writer.setOutput(new OutputStreamWriter(System.out));
rssreader.convertRSSToHtml(parser, writer);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}

public void convertRSSToHtml(XmlPullParser parser, XmlSerializer writer)
throws IOException, XmlPullParserException
{
// <!ELEMENT rss (channel)>
if (parser.nextTag() == XmlPullParser.START_TAG
&& parser.getName().equals("rss"))
{
writer.startTag(null,"html");
if (parser.nextTag() == XmlPullParser.START_TAG
&& parser.getName().equals("channel"))
{
convertChannelToHtml(parser, writer);
parser.require(XmlPullParser.END_TAG, null, "channel");
} else {
new RuntimeException("expectd channel start tag not "+parser.getPositionDescription());
}
parser.nextTag();
parser.require(XmlPullParser.END_TAG, null, "rss");
writer.endTag(null, "html");
writer.flush();
} else {
throw new RuntimeException("expectd an RSS document at" + parser.getPositionDescription());
}
}

public void convertChannelToHtml(XmlPullParser parser, XmlSerializer writer)
throws IOException, XmlPullParserException
{
// <!ELEMENT channel (title | description | link | language | item+ | rating? | image? | textinput? | copyright? | pubDate? | lastBuildDate? | docs? | managingEditor? | webMaster? | skipHours? | skipDays?)*>
boolean seenBody = false; //assumption that title is before items ...
while (parser.nextTag() != XmlPullParser.END_TAG) { // this guranteed by well formednes of XML && parser.getName().equals("channel"))) {
// if (parser.getEventType() == XmlPullParser.START_TAG) { //guranteed by nextTag
// <!ELEMENT title (#PCDATA)>
if(parser.getName().equals("title") && !seenBody) {
writer.startTag(null,"head");
writer.startTag(null,"title").text(parser.nextText()).endTag(null,"title");
writer.endTag(null,"head");
} else if(parser.getName().equals("item")) {
if(!seenBody) {
writer.startTag(null, "body");
seenBody = true;
}
convertItemToHtml(parser, writer);
} else {
// skip any element content including sub elements...
int level = 1;
while (level > 0) {
switch(parser.next()) {
case XmlPullParser.START_TAG: ++level; break;
case XmlPullParser.END_TAG: --level; break;
}
}
}
}
if(seenBody) writer.endTag(null, "body");
}

public void convertItemToHtml(XmlPullParser parser, XmlSerializer writer)
throws IOException, XmlPullParserException
{
writer.startTag(null, "p");
//<!ELEMENT item (title | link | description)*>
String title = null, link = null, description = null;
while (parser.nextTag() != XmlPullParser.END_TAG) {
if (parser.getName().equals("title")) {
title = parser.nextText();
} else if (parser.getName().equals("link")) {
link = parser.nextText();
} else if (parser.getName().equals("description")) {
description = parser.nextText();
}
}

//HashMap attributes = new HashMap(1);
//if(link != null) attributes.put("href", link);
//writer.beginElement("a",attributes);
writer.startTag(null, "a");
writer.attribute(null, "href", link);
if(title != null) {
writer.text(title);
} else {
writer.text(link);
}
writer.endTag(null,"a");

//writer.writeEmptyElement("br");
writer.startTag(null, "br").endTag(null, "br");

if(description != null) writer.text(description);

writer.endTag(null, "p"); // end the "p" element
}
}





// modified by Aleksander Slominski
// based on http://www.xml.com/pub/a/2002/05/22/parsing.html?page=2

import java.io.*;
import java.net.*;

import org.xmlpull.v1.*;

public class RSSReader {

public static void main(String [] args)
{

// create an instance of RSSReader
RSSReader rssreader = new RSSReader();

try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
String url = args[0];
InputStreamReader stream = new InputStreamReader(
new URL(url).openStream());
parser.setInput(stream);
XmlSerializer writer = factory.newSerializer();
writer.setOutput(new OutputStreamWriter(System.out));
rssreader.convertRSSToHtml(parser, writer);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}

public void convertRSSToHtml(XmlPullParser parser, XmlSerializer writer)
throws IOException, XmlPullParserException
{
// <!ELEMENT rss (channel)>
if (parser.nextTag() == XmlPullParser.START_TAG
&& parser.getName().equals("rss"))
{
writer.startTag(null,"html");
if (parser.nextTag() == XmlPullParser.START_TAG
&& parser.getName().equals("channel"))
{
convertChannelToHtml(parser, writer);
parser.require(XmlPullParser.END_TAG, null, "channel");
} else {
new RuntimeException("expectd channel start tag not "+parser.getPositionDescription());
}
parser.nextTag();
parser.require(XmlPullParser.END_TAG, null, "rss");
writer.endTag(null, "html");
writer.flush();
} else {
throw new RuntimeException("expectd an RSS document at" + parser.getPositionDescription());
}
}

回复 点赞
小小草 2005年03月31日
哦,我正做呢,给你一个例子吧!这个是生成rss文档,这个使用dom4j生成rss2.0文档
Document doc=DocumentHelper.createDocument();
Element rssElement=doc.addElement("rss");
rssElement.setAttributeValue("version","2.0");
Element chanelElement=rssElement.addElement("chanel");
Element ctitleElement=chanelElement.addElement("title");
ctitleElement.setText(custrs.get("cust_name"));

Element cdescriptionElement=chanelElement.addElement("description");
cdescriptionElement.addCDATA(custrs.get("description"));

Element clinkElement=chanelElement.addElement("link");
clinkElement.setText(PageURL.COMURL+PageURL.getENT(cust_id));

Element clastBuildDateElement=chanelElement.addElement("lastBuildDate");
clastBuildDateElement.setText(new java.util.Date().toString());

Element clanguageElement=chanelElement.addElement("language");
clanguageElement.setText("zh-cn");

Element ccopyrightElement=chanelElement.addElement("copyright");
ccopyrightElement.setText("Copyright (C); 2005, 51766.com");

//下边定义信息的标题,联接,发布日期,简要描述,作者等信息
Element citemElement=null;
Element titleElement=null;
Element linkElement =null;
Element pubDateElement=null;
Element descriptionElement=null;
Element authorElement=null;

for(int i=0;i<rs.size();i++){
//增加一个新的信息item元素
citemElement=chanelElement.addElement("item");
//创建title元素,并设置其内容
titleElement=citemElement.addElement("title");
titleElement.setText(rs.get(i,"title"));
//创建link元素,并设置其内容
linkElement=citemElement.addElement("link");
linkElement.setText(PageURL.COMURL+PageURL.getURL(rs.get(i,"info_id"),Info.NEWS));
//创建pubDate元素,并设置其内容
pubDateElement=citemElement.addElement("pubDate");
pubDateElement.setText(rs.get(i,"pub_date"));
//创建description元素,并设置其内容
descriptionElement=citemElement.addElement("description");
descriptionElement.addCDATA(rs.get(i,"title")+"["+custrs.get("cust_name")+"]");
//创建author元素,并设置其内容
authorElement=citemElement.addElement("author");
authorElement.setText(custrs.get("cust_name"));
}
org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
try {
//XMLWriter output = new XMLWriter(new FileWriter(new File("d:/catalog.xml")));//输出到文件
org.dom4j.io.XMLWriter output = new org.dom4j.io.XMLWriter(out,format); //输出到web
output.write(doc);
output.close();
} catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
回复 点赞
小蛀 2005年03月28日
RSS是一个规范,你只要了解什么是XML就可以了.

回复 点赞
surfchen 2005年03月26日
JAVA如何处理文本就如何处理XML~~
回复 点赞
myvicy 2005年03月25日
http://blog.yesky.com/Blog/yanhongda/archive/2005/02/25/79650.html
回复 点赞
usaspy 2005年03月25日
直接取数据库就是了,干嘛要定义xml标签,从xml文档来取数据呢
回复 点赞
usaspy 2005年03月25日
我想知道Rss中,java如何读取xml的数据
回复 点赞
发动态
发帖子
XML/XSL
创建于2007-09-28

7876

社区成员

2.2w+

社区内容

XML/XSL相关问题讨论专区
社区公告
暂无公告