Java Reference
In-Depth Information
Otherwise, add the item to the
attributes
collection.
else
{
if(node.getNodeType()!=Node.TEXT_NODE)
attributes.put(nodename, RSS.getXMLText(node));
}
}
The
attributes
collection stores all of the attributes that were loaded with the
<channel>
element.
Parse an RSS Format Date
The RSS standard specifies a very specific format for dates. The
parseDate
method
of the RSS class converts these dates to Java
Date
objects. As previously mentioned, the
following is the format that RSS expects a date to be in.
Sun, 8 Oct 2006 04:00:00 GMT
To parse the date, the
SimpleDateFormat
class of Java is used. The
SimpleDateFormat
object is constructed to accept a date that matches the above for-
mat. Once the date is parsed, it is returned.
try
{
DateFormat formatter =
new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date date = (Date) formatter.parse(datestr);
return date;
If an error occurs while parsing the date, then a value of
null
is returned for the date.
} catch (Exception e)
{
return null;
}
This method is made
static
so that it can be called directly from the
RSSItem
class.
The RSSItem Class
The
RSSItem
class is the class that holds individual RSS items, or articles. The
RSSItem
class also holds all of the attributes that were parsed from each of the
<item>
elements in the RSS feed. You may view the
RSSItem
class below in Listing 12.4.
Listing 12.4: The RSSItem Class (RSSItem.java)
package com.heatonresearch.httprecipes.rss;