Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



15.7 A Mobile RSS Client


The last example of XML parser use in this chapter is a mobile Really Simple Syndication (RSS) client. RSS is a widely used XML format for news and blog sites to feed their headline contents to aggregators. For news sites, RSS makes it possible to advertise their headlines and provides links back to their sites; for aggregators, the use of RSS avoids HTML screen scraping and makes it possible to automatically aggregate a large number of sites.


15.7.1 A Simple RSS Example


RSS is designed to be simple. It is readable by humans and can be easily parsed by machines. Resources" for more information).

Listing 15.8. A real simple RSS feed



<rss version="0.91">
<channel>
<title>My Site Content</title>
<link>http://www.mysite.com</link>
<description>Latest Content</description>
<language>en-us</language>
<copyright>Copyright 2003 myself</copyright>
<lastBuildDate>24/02/03 12:34:56</lastBuildDate>
<image>
<title>MySite</title>
<url>http://www.mysite.com/pic.gif</url>
<link>http://www.DevX.com</link>
</image>
<item>
<title>Good news</title>
<description>Product shipped</description>
<link>http://www.mysite.com/shipped</link>
<author>me@mysite.com</author>
<pubDate>22/02/03 11:22:33</pubDate>
</item>
<item>
<title> etc </title>
<description> etc </description>
<link> etc </link>
<pubDate> etc </pubDate>
</item>
</channel>
</rss>


15.7.2 PeekAndPick


Jonathan Knudsen's PeekAndPick (v2.0) is an MIDP RSS client. It aggregates headlines from a number of news/blog sites specified by the user. The user can read headlines and have the link emailed to him via the mobile phone (see Figure 15.5).


Figure 15.5. PeekAndPick in action.


The complete code and design documentation of PeekAndPick is available (see "Resources"). In the source code package, classes under the rss package provide an RSS parser based on kXML's XmlPull parser and its interface to other programming components. Listing 15.9 shows the code snippet from the kXML12Parser.parse() method which parses the RSS stream. The flow is very simple. The program goes into a channel element, iterates through the item elements, and reads out the contents in the title, link, and description nodes. All other advanced RSS elements are ignored.

Listing 15.9. The kXML12Parser.parse() method



public void parse(InputStream in)
throws IOException {
mCancel = false;
Reader reader = new InputStreamReader(in);
XmlParser parser = new XmlParser(reader);
ParseEvent pe = null;
parser.skip();
pe = parser.read();
String root = pe.getName();
if (root.equals("rss")) {
parser.skip();
parser.read(Xml.START_TAG, null, "channel");
}
boolean trucking = true;
boolean first = true;
while (trucking && mCancel == false) {
pe = parser.read();
if (pe.getType() == Xml.START_TAG) {
String name = pe.getName();
if (name.equals("item")) {
String title = null, link = null;
String description = null;
while ((pe.getType() != Xml.END_TAG) ||
(pe.getName().equals(name) == false)) {
pe = parser.read();
if (pe.getType() == Xml.START_TAG &&
pe.getName().equals("title")) {
pe = parser.read();
title = pe.getText();
} else if (pe.getType() == Xml.START_TAG
&& pe.getName().equals("link")) {
pe = parser.read();
link = pe.getText();
} else if (pe.getType() == Xml.START_TAG
&& pe.getName().equals("description")) {
pe = parser.read();
description = pe.getText();
}
}
if (first) {
if (mCancel == false) fireFirstItem();
first = false;
}
if (mCancel == false)
fireItemParsed(title, link, description);
} else {
while ((pe.getType() != Xml.END_TAG) ||
(pe.getName().equals(name) == false))
pe = parser.read();
}
}
if (pe.getType() == Xml.END_TAG &&
pe.getName().equals(root))
trucking = false;
}
if (mCancel == false) fireFinished();
mCancel = false;
}

The spirit of RSS is very similar to the concept of XML Web Services: offering services through a standard, interoperable interface. In the next chapter, we dive into SOAP XML Web Services.


/ 204