`

Android中的Atom学习

阅读更多

 

1.Atom的简单介绍:

由于RSS不是一个真正的开放的标准,而且存在这混乱的版本号,所以相关领域的专家就坐下来一起研发下一代的开放标准的格式。正是这样的一个目的造就了Atom最大的优点——开放标准。Atom是由庞大的委员会磋商而成的一个社区标准。

atom具体的标准请参阅官方网站

http://www.atomenabled.org/developers/syndication/atom-format-spec.php

 

2.Atom的规范格式:

|-AtomFEED常用属性

       |-Author         作者:必须包括一个名多个

       |-Category      类型:可能会很多

       |-Contributor   投稿者:可能会有很多个

       |-Generator     发生器:调用的程序,绝对不能是多个

       |-Icon             图标:绝对不能包含多个

       |-Id                必须包含一个准备的ID

       |-Link             链接:应该有个属性ref=self,因为此链接会作为首选地址!绝对不能有多个属性ref=alternate;也可以有额外的链接属性,但是它只是在其他的的基础上做描述

       |-Rights          版权:绝对不能有多个

       |-Subtitle        副标题:绝对不能有多个

       |-Title             主标题:必须是一个准确的标题

       |-Updated              最近更新时间:必须是一个准确的;

 

3.代码说明

3.1配置定位环境

因为要用到网络的资源,所以必须在AndroidManifest.xml里加入权限

<uses-permission android:name="android.permission.INTERNET" /> 

因为考虑到可能要读取不同的类型的xml文件,也就是兼容rssatom的各个版本,所以为了统一,采用了外部的jar包,其中有rome.jarjdom.jarrome.jar帮我们封装了处理不同类型的xml的分歧。统一了代码。

 

3.2.编写业务逻辑代码

(1) 编写ChannelVO

以下是Atom封装的类,封装了获得Atom的信息,将atom对象化

public class ChannelVO {
	private String title;//频道的标题
	private String description;//频道的描述
	private String link;//频道的连接
	private String uri;//频道的统一资源标示符
	@SuppressWarnings("unchecked")
	private List items;//所有的Item
	private String language;//标示的是语言
	private String copyright;//版权信息
	private Date pubDate;//发布日趋
	private String feedType;//类型
	private String encoding;//编码类型

……
	//下边的是上边的属性的一系列get(),set()方法
	……
}

 (2) 编写的ChannelItemVO封转了Item的信息,将Item对象化。

public class ChannelItemVO {
	private String title;// Rss文件中Item的标题
	private String link;// Rss文件中Item对应的连接
	private String description;// Item的描述
	private Date pubDate;// Item发布的时间
	private String author;// Item作者
private String category;// Item所属的频道范畴

……
	//下边的是上边的属性的一系列get(),set()方法
	……
}

 (3)编写测试类FeedWriter通过romejar包可以很容易的实现添加feed,通过SyndFeed的实例可以创建feed种子,然后通过各种的set()方法,把要添加的内容一个个的添加到feed上,最后流的形式建立新的xml文件。

String feedType = args[0];
String fileName = args[1];
DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);
SyndFeed feed = new SyndFeedImpl(); // feed流
feed.setFeedType(feedType); // 设置rss版本
feed.setTitle("Sample Feed (created with Rome)"); // <title>设置标题
feed.setLink("http://www.csdn.net"); // <link>
feed.setDescription("This feed has been created using Rome (Java syndication utilities");
List<SyndEntryImpl> entries = new ArrayList<SyndEntryImpl>();
SyndEntry entry;
SyndContent description;
entry = new SyndEntryImpl(); // 子节点
entry.setTitle("Rome v1.0");
entry.setLink("http://www.csdn.net");
entry.setPublishedDate(dateParser.parse("2004-06-08"));
description = new SyndContentImpl();
description.setType("text/plain");
description.setValue("Initial release of Rome");
entry.setDescription(description);
entries.add((SyndEntryImpl) entry);
entry = new SyndEntryImpl();
entry.setTitle("Rome v2.0");
entry.setLink("http://ww.csdn.net");
entry.setPublishedDate(dateParser.parse("2004-06-16"));
description = new SyndContentImpl(); // 描述
description.setType("text/xml");
description.setValue("Bug fixes, < xml>XML< /xml> minor API changes and some new features");
entry.setDescription(description);
entries.add((SyndEntryImpl) entry);
entry = new SyndEntryImpl();
entry.setTitle("Rome v3.0");
entry.setLink("http://www.csdn.net");
entry.setPublishedDate(dateParser.parse("2004-07-27"));
description = new SyndContentImpl();
description.setType("text/html");
description.setValue("< p>More Bug fixes, mor API changes, some new features and some Unit testing< /p>"
				+ "< p>For details check the < a href=\"http://www.csdn.net\">Changes Log< /a>< /p>");
entry.setDescription(description);
entries.add((SyndEntryImpl) entry);
feed.setEntries(entries); // 设置子节点
Writer writer = new FileWriter(fileName);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer); // 写到文件中去
writer.close(); 

 (4)ReaderRSS是测试是否可以兼容的浏览不同的xml文件。通过本地的三个不同的文件:

String url = "http://10.167.12.184:8080/examples/images/RSS1.0.xml";
String url = "http://10.167.12.184:8080/examples/images/RSS2.0.xml";
String url = "http://10.167.12.184:8080/examples/images/ATOM.xml";

 测试出rome已经帮我们封装好了我们可以不用关心xml的类型

(5)通过sax解析也能够直接的读取atomfeed方法和rss一样。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics