RSS feeds with Java 原文来自:http://www.javaarch.net/jiagoushi/479.htm
-
RSS - 简易信息聚合
RSS文档是一种XML格式的文件,遵循RSS规范,我们可以使用java的xml操作来读写RSS文档。
2.RSS的工程和领域模型
/* * Represents one RSS message */ public class FeedMessage { String title; String description; String link; String author; String guid; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getGuid() { return guid; } public void setGuid(String guid) { this.guid = guid; } @Override public String toString() { return "FeedMessage [title=" + title + ", description=" + description + ", link=" + link + ", author=" + author + ", guid=" + guid + "]"; } }
Feed.java
import java.util.ArrayList; import java.util.List; /* * Stores an RSS feed */ public class Feed { final String title; final String link; final String description; final String language; final String copyright; final String pubDate; final List<FeedMessage> entries = new ArrayList<FeedMessage>(); public Feed(String title, String link, String description, String language, String copyright, String pubDate) { this.title = title; this.link = link; this.description = description; this.language = language; this.copyright = copyright; this.pubDate = pubDate; } public List<FeedMessage> getMessages() { return entries; } public String getTitle() { return title; } public String getLink() { return link; } public String getDescription() { return description; } public String getLanguage() { return language; } public String getCopyright() { return copyright; } public String getPubDate() { return pubDate; } @Override public String toString() { return "Feed [copyright=" + copyright + ", description=" + description + ", language=" + language + ", link=" + link + ", pubDate=" + pubDate + ", title=" + title + "]"; } }
3.使用Stax读取RSS feed
RSSFeedParser.java
import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Characters; import javax.xml.stream.events.XMLEvent; import de.vogella.rss.model.Feed; import de.vogella.rss.model.FeedMessage; public class RSSFeedParser { static final String TITLE = "title"; static final String DESCRIPTION = "description"; static final String CHANNEL = "channel"; static final String LANGUAGE = "language"; static final String COPYRIGHT = "copyright"; static final String LINK = "link"; static final String AUTHOR = "author"; static final String ITEM = "item"; static final String PUB_DATE = "pubDate"; static final String GUID = "guid"; final URL url; public RSSFeedParser(String feedUrl) { try { this.url = new URL(feedUrl); } catch (MalformedURLException e) { throw new RuntimeException(e); } } public Feed readFeed() { Feed feed = null; try { boolean isFeedHeader = true; // Set header values intial to the empty string String description = ""; String title = ""; String link = ""; String language = ""; String copyright = ""; String author = ""; String pubdate = ""; String guid = ""; // First create a new XMLInputFactory XMLInputFactory inputFactory = XMLInputFactory.newInstance(); // Setup a new eventReader InputStream in = read(); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); // Read the XML document while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { String localPart = event.asStartElement().getName() .getLocalPart(); switch (localPart) { case ITEM: if (isFeedHeader) { isFeedHeader = false; feed = new Feed(title, link, description, language, copyright, pubdate); } event = eventReader.nextEvent(); break; case TITLE: title = getCharacterData(event, eventReader); break; case DESCRIPTION: description = getCharacterData(event, eventReader); break; case LINK: link = getCharacterData(event, eventReader); break; case GUID: guid = getCharacterData(event, eventReader); break; case LANGUAGE: language = getCharacterData(event, eventReader); break; case AUTHOR: author = getCharacterData(event, eventReader); break; case PUB_DATE: pubdate = getCharacterData(event, eventReader); break; case COPYRIGHT: copyright = getCharacterData(event, eventReader); break; } } else if (event.isEndElement()) { if (event.asEndElement().getName().getLocalPart() == (ITEM)) { FeedMessage message = new FeedMessage(); message.setAuthor(author); message.setDescription(description); message.setGuid(guid); message.setLink(link); message.setTitle(title); feed.getMessages().add(message); event = eventReader.nextEvent(); continue; } } } } catch (XMLStreamException e) { throw new RuntimeException(e); } return feed; } private String getCharacterData(XMLEvent event, XMLEventReader eventReader) throws XMLStreamException { String result = ""; event = eventReader.nextEvent(); if (event instanceof Characters) { result = event.asCharacters().getData(); } return result; } private InputStream read() { try { return url.openStream(); } catch (IOException e) { throw new RuntimeException(e); } } }
ReadTest.java测试读RSS文档
public class ReadTest {
public static void main(String[] args) {
RSSFeedParser parser = new RSSFeedParser("http://www.vogella.de/article.rss");
Feed feed = parser.readFeed();
System.out.println(feed);
for (FeedMessage message : feed.getMessages()) {
System.out.println(message);
}
}
}
4.使用Stax来生成RSS文档
import java.io.FileOutputStream; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartDocument; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import de.vogella.rss.model.Feed; import de.vogella.rss.model.FeedMessage; public class RSSFeedWriter { private String outputFile; private Feed rssfeed; public RSSFeedWriter(Feed rssfeed, String outputFile) { this.rssfeed = rssfeed; this.outputFile = outputFile; } public void write() throws Exception { // Create a XMLOutputFactory XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); // Create XMLEventWriter XMLEventWriter eventWriter = outputFactory .createXMLEventWriter(new FileOutputStream(outputFile)); // Create a EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create open tag eventWriter.add(end); StartElement rssStart = eventFactory.createStartElement("", "", "rss"); eventWriter.add(rssStart); eventWriter.add(eventFactory.createAttribute("version", "2.0")); eventWriter.add(end); eventWriter.add(eventFactory.createStartElement("", "", "channel")); eventWriter.add(end); // Write the different nodes createNode(eventWriter, "title", rssfeed.getTitle()); createNode(eventWriter, "link", rssfeed.getLink()); createNode(eventWriter, "description", rssfeed.getDescription()); createNode(eventWriter, "language", rssfeed.getLanguage()); createNode(eventWriter, "copyright", rssfeed.getCopyright()); createNode(eventWriter, "pubdate", rssfeed.getPubDate()); for (FeedMessage entry : rssfeed.getMessages()) { eventWriter.add(eventFactory.createStartElement("", "", "item")); eventWriter.add(end); createNode(eventWriter, "title", entry.getTitle()); createNode(eventWriter, "description", entry.getDescription()); createNode(eventWriter, "link", entry.getLink()); createNode(eventWriter, "author", entry.getAuthor()); createNode(eventWriter, "guid", entry.getGuid()); eventWriter.add(end); eventWriter.add(eventFactory.createEndElement("", "", "item")); eventWriter.add(end); } eventWriter.add(end); eventWriter.add(eventFactory.createEndElement("", "", "channel")); eventWriter.add(end); eventWriter.add(eventFactory.createEndElement("", "", "rss")); eventWriter.add(end); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); } private void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException { XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); XMLEvent tab = eventFactory.createDTD("\t"); // Create Start node StartElement sElement = eventFactory.createStartElement("", "", name); eventWriter.add(tab); eventWriter.add(sElement); // Create Content Characters characters = eventFactory.createCharacters(value); eventWriter.add(characters); // Create End node EndElement eElement = eventFactory.createEndElement("", "", name); eventWriter.add(eElement); eventWriter.add(end); } }
WriteTest.java
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import de.vogella.rss.model.Feed; import de.vogella.rss.model.FeedMessage; import de.vogella.rss.write.RSSFeedWriter; public class WriteTest { public static void main(String[] args) { // Create the rss feed String copyright = "Copyright hold by Lars Vogel"; String title = "Eclipse and Java Information"; String description = "Eclipse and Java Information"; String language = "en"; String link = "http://www.vogella.de"; Calendar cal = new GregorianCalendar(); Date creationDate = cal.getTime(); SimpleDateFormat date_format = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US); String pubdate = date_format.format(creationDate); Feed rssFeeder = new Feed(title, link, description, language, copyright, pubdate); // Now add one example entry FeedMessage feed = new FeedMessage(); feed.setTitle("RSSFeed"); feed.setDescription("This is a description"); feed.setAuthor("nonsense@somewhere.de (Lars Vogel)"); feed.setGuid("http://www.vogella.de/articles/RSSFeed/article.html"); feed.setLink("http://www.vogella.de/articles/RSSFeed/article.html"); rssFeeder.getMessages().add(feed); // Now write the file RSSFeedWriter writer = new RSSFeedWriter(rssFeeder, "articles.rss"); try { writer.write(); } catch (Exception e) { e.printStackTrace(); } } }
相关推荐
本文将深入探讨RSS阅读器的工作原理、优势以及如何使用RSS_Feeds.widget这款便利的RSS阅读器来提升你的信息获取效率。 首先,了解RSS阅读器的基本功能。RSS阅读器的主要任务是收集并整理来自RSS源的信息,这些源...
RSSJava提供了一组API来创建、读取和写入RSS feeds,而Rome则是一个更全面的库,支持RSS和Atom格式,包含了解析、生成和订阅功能。 7. **RSS更新通知**: 虽然RSS本身不提供更新通知机制,但可以通过结合Webhooks...
【标题】"rssfeeds-main.rar" 是一个压缩文件,它很可能包含了RSS(Really Simple Syndication)相关的源代码或数据。RSS是一种用于发布和聚合新闻、博客和其他类型在线内容的XML格式。通过解析RSS feed,用户可以...
RSS Submit Pro是一个RSS feeds提交工具,可以自动想主要的RSS网站提交RSS feed。它使用方便,速度也很快,是一个不错的网站推广工具。 RSS Submit是一个自动将你的RSS Feed提交到各大RSS搜索引擎或者索引的工具,...
这些库提供了一套API,用于创建、解析和操作RSS feeds。 5. **HTTP请求**:为了获取RSS源,需要发送HTTP请求。Java内置的`java.net.URL`和`java.net.HttpURLConnection`类可以用来实现,或者使用Apache HttpClient...
Rome是Java社区中一个流行的RSS和Atom聚合库,它提供了一系列类和方法,用于解析、创建、修改RSS和Atom feeds。 描述中提到的“java相关RSS 部分资料已经生成代码和jar包”,意味着这个压缩包内不仅包含Rome库的jar...
Java RSS阅读器是一款基于Java开发的课程大作业项目,它主要功能是解析RSS(Really Simple Syndication) feeds,根据用户提供的URL,能够定向地读取RSS源中的新闻标题、摘要、发布日期等信息。RSS是一种内容聚合...
【标题】:“rssfeeds:结合rss feeds” 在互联网上,RSS(Really Simple Syndication)是一种内容聚合格式,它允许用户订阅网站更新,而无需频繁访问各个站点。RSS feeds通过XML编码提供,通常以.xml或.rss扩展名...
描述中的“Translate RSS feeds into your language”进一步强调了该工具的主要功能,即用户可以根据自己的需求,将接收到的RSS源内容转换成自己熟悉的语言,这在浏览不同语言的新闻、博客或其他信息源时非常有用。...
除了Rome和rssutils,还有其他库可供选择,例如`rsslib4j`,它也是一个Java库,用于处理RSS和Atom feeds,提供了RSS解析、创建和发布的能力。 在使用这些库进行RSS采集时,开发者需要考虑以下几点: - **兼容性**:...
Rome是一个Java库,它为创建、聚合和消费RSS和Atom feeds提供了一套API。这个库包括了RSS和Atom规范的模型类,以及解析和生成XML文档的工具。要使用Rome,你需要将其添加到你的项目依赖中。如果你使用的是Maven,...
RSS(Really Simple Syndication)是一种用于发布和订阅新闻、博客文章等在线内容的格式,而Java作为一门广泛使用的编程语言,可以提供强大的功能来解析RSS feeds并呈现给用户。XML则是数据交换和存储的标准格式,它...
Java RSS开发是一个重要的领域,主要用于创建、解析和发布RSS(Really Simple Syndication) feeds,这是一种标准格式,用于网站内容的聚合和更新。本压缩包"java_rss.rar"包含了几个关键库,帮助开发者处理RSS相关...
【标题】中的“Atom-feeds”是指一个基于Golang(Go语言)的RSS/Atom生成器库,由gorilla团队开发。RSS(Really Simple Syndication)和Atom是两种广泛使用的互联网内容聚合格式,用于发布博客、新闻和其他定期更新...
Java and XML, 3rd Edition, shows you how ... You'll learn how to create, read, and modify RSS feeds for syndicated content and use XML to power the next generation of websites with Ajax and Adobe Flash.
Java+XML编写的RSS阅读器是一个利用Java编程语言和XML技术实现的应用,主要用于解析和展示RSS(Really Simple Syndication) feeds。RSS是一种基于XML的互联网内容发布格式,它允许用户订阅并获取网站的最新更新,如...
ROME是一个开源的Java库,它为处理RSS和Atom feeds提供了一种简单的方法。它包含了RSS和Atom feed的解析、生成以及模型化等功能,使得开发者能够方便地与这些格式进行交互。 其次,JDOM是Java文档对象模型,它是...
Rome提供了一系列的类和接口,可以方便地创建、解析和操纵RSS和Atom feeds。例如,`RssReader`类用于读取RSS源,` SyndFeed`类代表一个聚合,包含各种频道信息和条目。通过这个库,开发者可以轻松地获取RSS馈送中的...
Content Syndication with RSS offers webloggers, developers, and the programmers who support them a thorough explanation of syndication in general and RSS in particular. Written for web developers who...
标题中的“利用Fix Rss Feeds插件修复WordPress的Feed显示错误”指的是在WordPress网站上,用户遇到RSS馈送(Feed)无法正常显示的问题。这个问题可能是由于多种原因引起的,包括但不限于编码错误、文件格式问题或者...