Java Reference
In-Depth Information
How it works...
In this example, the code retrieves weather information from Yahoo Weather as RSS-encoded
data. The steps needed to process RSS feeds are similar to those used in XML processing
(see previous recipe). Let's examine how this is done:
F
Model the weather
—the first item listed is the definition of class
Weather
.
It models the weather information that will be transferred from the server.
A script-level instance of
Weather
is assigned to variable
weather
, which
will be used to store information extracted from the RSS feed in function
loadRssInfo()
(see next bullet).
F
Retrieving RSS data
—function
loadRssInfo(zip:String)
encapsulates the code
necessary to send a request to the feeds server and process the response. To do this,
it declares an instance of
RssTask
, which is part of JavaFX's Feeds API.
RssTask
uses the PullParser API internally to process RSS feeds.
Therefore, it is an event-based parser that raises parsing events as
the document is streamed from its source. To use the
RssTask
, you
first set the
location
property to point to the RSS resource that
you want to request. Property
onForeignEvent
is used to specify
a callback function to handle streaming events used to extract
weather information encoded in non-standard RSS tags, as is done
in the Yahoo Weather format.
The same strategy used when processing XML in the previous recipe is used
here. The code sets up a chain of
if
expressions designed to test when an expected
element is encountered. When that happens, the code then retrieves the RSS
element's attribute value, as shown in the following code snippet that retrieves
attribute
"speed"
from element
"wind"
. The retrieved value is then assigned
to the
weather
object.
if(
event.type
==
PullParser.END_ELEMENT
and
event.qname.name.equals("
wind
")){
weather.windSpeed=event.getAttributeValue("speed")
}



